Permalink
Browse files

Implementation of #250, #242, #249, #136

  • Loading branch information...
FabianKramm committed Sep 25, 2018
1 parent 63ce92a commit ac268f131767218423d0d71fa1dae25b0810bdea
View
@@ -15,6 +15,6 @@
# History Plugin for VS Code
.history/
devspace
/devspace
debug
.DS_STORE
View
@@ -57,11 +57,12 @@ type UpCmdFlags struct {
open string
initRegistries bool
build bool
shell string
sync bool
deploy bool
portforwarding bool
noSleep bool
verboseSync bool
container string
}
//UpFlagsDefault are the default flags for UpCmdFlags
@@ -74,6 +75,8 @@ var UpFlagsDefault = &UpCmdFlags{
deploy: false,
portforwarding: true,
noSleep: false,
verboseSync: false,
container: "",
}
const clusterRoleBindingName = "devspace-users"
@@ -97,16 +100,16 @@ Starts and connects your DevSpace:
4. Starts the sync client
5. Enters the container shell
#######################################################`,
Args: cobra.NoArgs,
Run: cmd.Run,
Run: cmd.Run,
}
rootCmd.AddCommand(cobraCmd)
cobraCmd.Flags().BoolVar(&cmd.flags.tiller, "tiller", cmd.flags.tiller, "Install/upgrade tiller")
cobraCmd.Flags().BoolVar(&cmd.flags.initRegistries, "init-registries", cmd.flags.initRegistries, "Initialize registries (and install internal one)")
cobraCmd.Flags().BoolVarP(&cmd.flags.build, "build", "b", cmd.flags.build, "Build image if Dockerfile has been modified")
cobraCmd.Flags().StringVarP(&cmd.flags.shell, "shell", "s", "", "Shell command (default: bash, fallback: sh)")
cobraCmd.Flags().StringVarP(&cmd.flags.container, "container", "c", cmd.flags.container, "Container name where to open the shell")
cobraCmd.Flags().BoolVar(&cmd.flags.sync, "sync", cmd.flags.sync, "Enable code synchronization")
cobraCmd.Flags().BoolVar(&cmd.flags.verboseSync, "verbose-sync", cmd.flags.verboseSync, "When enabled the sync will log every file change")
cobraCmd.Flags().BoolVar(&cmd.flags.portforwarding, "portforwarding", cmd.flags.portforwarding, "Enable port forwarding")
cobraCmd.Flags().BoolVarP(&cmd.flags.deploy, "deploy", "d", cmd.flags.deploy, "Deploy chart")
cobraCmd.Flags().BoolVar(&cmd.flags.noSleep, "no-sleep", cmd.flags.noSleep, "Enable no-sleep")
@@ -180,7 +183,7 @@ func (cmd *UpCmd) Run(cobraCmd *cobra.Command, args []string) {
}()
}
cmd.enterTerminal()
cmd.enterTerminal(args)
}
func (cmd *UpCmd) ensureNamespace() error {
@@ -675,12 +678,36 @@ func (cmd *UpCmd) startSync() []*synctool.SyncConfig {
if err != nil {
log.Panicf("Unable to list devspace pods: %s", err.Error())
} else if pod != nil {
if len(pod.Spec.Containers) == 0 {
log.Warnf("Cannot start sync on pod, because selected pod %s/%s has no containers", pod.Namespace, pod.Name)
continue
}
container := &pod.Spec.Containers[0]
if syncPath.ContainerName != nil && *syncPath.ContainerName != "" {
found := false
for _, c := range pod.Spec.Containers {
if c.Name == *syncPath.ContainerName {
container = &c
found = true
break
}
}
if found == false {
log.Warnf("Couldn't start sync, because container %s wasn't found in pod %s/%s", *syncPath.ContainerName, pod.Namespace, pod.Name)
continue
}
}
syncConfig := &synctool.SyncConfig{
Kubectl: cmd.kubectl,
Pod: pod,
Container: &pod.Spec.Containers[0],
Container: container,
WatchPath: absLocalPath,
DestPath: *syncPath.ContainerPath,
Verbose: cmd.flags.verboseSync,
}
if syncPath.ExcludePaths != nil {
@@ -700,7 +727,7 @@ func (cmd *UpCmd) startSync() []*synctool.SyncConfig {
log.Fatalf("Sync error: %s", err.Error())
}
log.Donef("Sync started on %s <-> %s", absLocalPath, *syncPath.ContainerPath)
log.Donef("Sync started on %s <-> %s (Pod: %s/%s)", absLocalPath, *syncPath.ContainerPath, pod.Namespace, pod.Name)
syncConfigs = append(syncConfigs, syncConfig)
}
}
@@ -756,24 +783,37 @@ func (cmd *UpCmd) startPortForwarding() {
}
}
func (cmd *UpCmd) enterTerminal() {
var shell []string
func (cmd *UpCmd) enterTerminal(args []string) {
var command []string
config := configutil.GetConfig(false)
if len(cmd.flags.shell) == 0 {
shell = []string{
if len(args) == 0 && (config.DevSpace.Terminal.Command == nil || len(*config.DevSpace.Terminal.Command) == 0) {
command = []string{
"sh",
"-c",
"command -v bash >/dev/null 2>&1 && exec bash || exec sh",
}
} else {
shell = []string{cmd.flags.shell}
if len(args) > 0 {
command = args
} else {
for _, cmd := range *config.DevSpace.Terminal.Command {
command = append(command, *cmd)
}
}
}
_, _, _, terminalErr := kubectl.Exec(cmd.kubectl, cmd.pod, cmd.pod.Spec.Containers[0].Name, shell, true, nil)
containerName := cmd.pod.Spec.Containers[0].Name
if cmd.flags.container != "" {
containerName = cmd.flags.container
} else if config.DevSpace.Terminal.ContainerName != nil {
containerName = *config.DevSpace.Terminal.ContainerName
}
_, _, _, terminalErr := kubectl.Exec(cmd.kubectl, cmd.pod, containerName, command, true, nil)
if terminalErr != nil {
if _, ok := terminalErr.(kubectlExec.CodeExitError); ok == false {
log.Fatalf("Unable to start terminal session: %s", terminalErr.Error())
log.Fatalf("Unable to start terminal session: %v", terminalErr)
}
}
}
@@ -10,6 +10,7 @@ func makeConfig() *v1.Config {
User: &v1.ClusterUser{},
},
DevSpace: &v1.DevSpaceConfig{
Terminal: &v1.Terminal{},
PortForwarding: &[]*v1.PortForwardingConfig{},
Release: &v1.Release{},
Sync: &[]*v1.SyncConfig{},
@@ -2,6 +2,7 @@ package v1
//DevSpaceConfig defines the devspace deployment
type DevSpaceConfig struct {
Terminal *Terminal `yaml:"terminal"`
Release *Release `yaml:"release"`
PortForwarding *[]*PortForwardingConfig `yaml:"portForwarding"`
Sync *[]*SyncConfig `yaml:"sync"`
@@ -28,6 +29,7 @@ type SyncConfig struct {
LabelSelector *map[string]*string `yaml:"labelSelector"`
LocalSubPath *string `yaml:"localSubPath"`
ContainerPath *string `yaml:"containerPath"`
ContainerName *string `yaml:"containerName"`
ExcludePaths *[]string `yaml:"excludePaths"`
DownloadExcludePaths *[]string `yaml:"downloadExcludePaths"`
UploadExcludePaths *[]string `yaml:"uploadExcludePaths"`
@@ -0,0 +1,7 @@
package v1
// Terminal describes the terminal options
type Terminal struct {
ContainerName *string `yaml:"containerName"`
Command *[]*string `yaml:"shell"`
}
@@ -216,7 +216,7 @@ func (d *downstream) downloadFiles(files []*fileInformation) (string, error) {
// Each file is represented in one line
for _, element := range files {
if lenFiles <= 3 || d.config.verbose {
if lenFiles <= 3 || d.config.Verbose {
d.config.Logf("[Downstream] Download file %s, size: %d", element.Name, element.Size)
}
@@ -335,7 +335,7 @@ func (d *downstream) removeFilesAndFolders(removeFiles map[string]*fileInformati
absFilepath := filepath.Join(d.config.WatchPath, key)
if shouldRemoveLocal(absFilepath, value, d.config) {
if numRemoveFiles <= 3 || d.config.verbose {
if numRemoveFiles <= 3 || d.config.Verbose {
d.config.Logf("[Downstream] Remove %s", key)
}
@@ -369,7 +369,7 @@ func (d *downstream) createFolders(createFolders []*fileInformation) {
for _, element := range createFolders {
if element.IsDirectory {
if numCreateFolders <= 3 || d.config.verbose {
if numCreateFolders <= 3 || d.config.Verbose {
d.config.Logln("[Downstream] Create folder: " + element.Name)
}
@@ -39,6 +39,7 @@ type SyncConfig struct {
ExcludePaths []string
DownloadExcludePaths []string
UploadExcludePaths []string
Verbose bool
fileIndex *fileIndex
@@ -51,9 +52,7 @@ type SyncConfig struct {
upstream *upstream
downstream *downstream
silent bool
verbose bool
silent bool
stopOnce sync.Once
// Used for testing
@@ -270,7 +270,7 @@ func (u *upstream) applyCreates(files []*fileInformation) error {
}
// Print changes
if u.config.verbose {
if u.config.Verbose {
for _, c := range writtenFiles {
if c.IsDirectory {
u.config.Logf("[Upstream] Create Folder %s", c.Name)
@@ -387,7 +387,7 @@ func (u *upstream) applyRemoves(files []*fileInformation) error {
}
// Print changes
if u.config.verbose {
if u.config.Verbose {
u.config.Logf("[Upstream] Remove %s", relativePath)
}
}

0 comments on commit ac268f1

Please sign in to comment.