func HandleSSH(argv []string) error { usage := `Open an SSH session on the specified environment Usage: sqrrl ssh <environment> ` args, err := docopt.Parse(usage, argv, true, "", false) if err != nil { return err } env := args["<environment>"].(string) cfg, err := config.LoadConfig("") if err != nil { return err } dpl := cfg.Deploy[env] if dpl == (config.Deploy{}) { return fmt.Errorf("failed to find configuration for the environment %s", env) } session, err := sshkit.NewSession(cfg, env) if err != nil { return err } return session.Exec() }
func HandleDeploy(argv []string) error { usage := `Deploy to the specified environment Usage: sqrrl deploy <environment> Options: -h, --help show this help screen -v, --verbose show command stdout/stderr ` args, err := docopt.Parse(usage, argv, true, "", false) if err != nil { return err } env := args["<environment>"].(string) cfg, err := config.LoadConfig("") if err != nil { return err } dpl := cfg.Deploy[env] if dpl == (config.Deploy{}) { return fmt.Errorf("failed to find configuration for the environment %s", env) } session, err := sshkit.NewSession(cfg, env) if err != nil { return err } session.Command = fmt.Sprintf("cd %s/source && git fetch --all", dpl.Path) if rc, err := session.Run(); err != nil && rc != 0 { return fmt.Errorf("failed to fetch updates: %v", err) } ref := dpl.Ref if ref == "" { ref = "origin/master" } session.Command = fmt.Sprintf("cd %s/source && git reset --hard %s", dpl.Path, ref) if rc, err := session.Run(); err != nil && rc != 0 { return fmt.Errorf("failed to reset git head: %v", err) } session.Command = fmt.Sprintf("ln -sfn %s/source %s/current", dpl.Path, dpl.Path) if rc, err := session.Run(); err != nil && rc != 0 { return fmt.Errorf("failed to symlink current source: %v", err) } return nil }
func HandleSetup(argv []string) error { usage := `Setup an environment for deployment Usage: sqrrl setup <environment> Options: -h, --help show this help screen -v, --verbose show command stdout/stderr ` args, err := docopt.Parse(usage, argv, true, "", false) if err != nil { return err } env := args["<environment>"].(string) cfg, err := config.LoadConfig("") if err != nil { return err } dpl := cfg.Deploy[env] if dpl == (config.Deploy{}) { return fmt.Errorf("failed to find configuration for the environment %s", env) } session, err := sshkit.NewSession(cfg, env) if err != nil { return err } session.Command = fmt.Sprintf("mkdir -p %s/{shared/{logs,pids},source}", dpl.Path) if rc, err := session.Run(); err != nil && rc != 0 { return fmt.Errorf("failed to setup path: %v", err) } session.Command = fmt.Sprintf("git clone %s %s/source", dpl.Repo, dpl.Path) if rc, err := session.Run(); err != nil && rc != 0 { return fmt.Errorf("failed to clone the repository: %v", err) } session.Command = fmt.Sprintf("ln -sfn %s/source %s/current", dpl.Path, dpl.Path) if rc, err := session.Run(); err != nil && rc != 0 { return fmt.Errorf("failed to symlink current source: %v", err) } return nil }