Skip to content

Commit

Permalink
feat(worker): worker key install --env (#3482)
Browse files Browse the repository at this point in the history
close #3476

Signed-off-by: Yvonnick Esnault <[email protected]>
  • Loading branch information
yesnault authored Oct 23, 2018
1 parent 45f353e commit b808ed5
Showing 1 changed file with 43 additions and 4 deletions.
47 changes: 43 additions & 4 deletions engine/worker/cmd_key.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,19 +36,49 @@ func cmdKey(w *currentWorker) *cobra.Command {
return cmdKeyRoot
}

var (
cmdInstallEnvGIT bool
cmdInstallEnv bool
)

func cmdKeyInstall(w *currentWorker) *cobra.Command {
c := &cobra.Command{
Use: "install",
Aliases: []string{"i", "add"},
Short: "worker key install <key-name>",
Short: "worker key install [--env-git] [--env] <key-name>",
Long: `
Inside a step script you can install a SSH/PGP key generated in CDS in your ssh environment and return the PKEY variable (only for SSH)
So if you want to update your PKEY variable, which is the variable with the path to the SSH private key you just can write ` + "PKEY=`worker key install proj-mykey`" + ` (only for SSH)
`,
So if you want to update your PKEY variable, which is the variable with the path to the SSH private key you just can write ` + "PKEY=$(worker key install proj-mykey)`" + ` (only for SSH)
You can use the ` + "`--env`" + ` flag to export the PKEY variable:
` + "```" + `
$ eval $(worker key install --env proj-mykey)
echo $PKEY # variable $PKEY will contains the path of the SSH private key
` + "```" + `
For most advanced usage with git and SSH, you can run ` + "`eval $(worker key install --env-git proj-mykey)`" + `.
The ` + "`--env-git`" + ` flag will display:
` + "```" + `
$ worker key install --env-git proj-mykey
echo "ssh -i /tmp/5/0/2569/655/bd925028e70aea34/cds.key.proj-mykey.priv -o StrictHostKeyChecking=no \$@" > /tmp/5/0/2569/655/bd925028e70aea34/cds.key.proj-mykey.priv.gitssh.sh;
chmod +x /tmp/5/0/2569/655/bd925028e70aea34/cds.key.proj-mykey.priv.gitssh.sh;
export GIT_SSH="/tmp/5/0/2569/655/bd925028e70aea34/cds.key.proj-mykey.priv.gitssh.sh";
export PKEY="/tmp/5/0/2569/655/bd925028e70aea34/cds.key.proj-mykey.priv";
` + "```" + `
So that, you can use custom git commands the the previous installed SSH key.
`,
Example: "worker key install proj-test",
Run: keyInstallCmd(w),
}
c.Flags().BoolVar(&cmdInstallEnvGIT, "env", false, "display shell command for export $PKEY variable. See documentation.")
c.Flags().BoolVar(&cmdInstallEnv, "env-git", false, "display shell command for advanced usage with git. See documentation.")
return c
}

Expand Down Expand Up @@ -107,11 +137,20 @@ func keyInstallCmd(w *currentWorker) func(cmd *cobra.Command, args []string) {

switch keyResp.Type {
case sdk.KeyTypeSSH:
if cmdInstallEnvGIT {
fmt.Printf("echo \"ssh -i %s -o StrictHostKeyChecking=no \\$@\" > %s.gitssh.sh;\n", keyResp.PKey, keyResp.PKey)
fmt.Printf("chmod +x %s.gitssh.sh;\n", keyResp.PKey)
fmt.Printf("export GIT_SSH=\"%s.gitssh.sh\";\n", keyResp.PKey)
fmt.Printf("export PKEY=\"%s\";\n", keyResp.PKey)
return
} else if cmdInstallEnv {
fmt.Printf("export PKEY=\"%s\";\n", keyResp.PKey)
return
}
fmt.Println(keyResp.PKey)
case sdk.KeyTypePGP:
fmt.Println("Your PGP key is imported with success")
}

}
}

Expand Down

0 comments on commit b808ed5

Please sign in to comment.