-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add install command and update readme
- Loading branch information
Showing
4 changed files
with
232 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,38 +1,128 @@ | ||
package ansible | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"path/filepath" | ||
) | ||
|
||
// Options for installing ansible | ||
type InstallOptions struct { | ||
Version string | ||
Method string | ||
Sudo bool | ||
} | ||
|
||
// Options for installing ansible via pip | ||
type InstallViaPip struct { | ||
RequirementsTXT string | ||
VirtualEnv string | ||
InstallOptions | ||
Path string | ||
} | ||
|
||
func InstallAnsible() error { | ||
func InstallViaPip(options *InstallOptions) error { | ||
// TODO | ||
return nil | ||
} | ||
var ( | ||
virtualenvBinary string | ||
virtualEnv string | ||
pipBinary string | ||
requirementsTXT string | ||
err error | ||
) | ||
|
||
func installAnsibleFromPip() error { | ||
// TODO | ||
return nil | ||
} | ||
if options.Path != "" { | ||
if _, err = os.Stat(options.Path); os.IsNotExist(err) { | ||
return fmt.Errorf("The specified Path (%s) does not exist", options.Path) | ||
} | ||
} | ||
|
||
func installAnsibleFromApt() error { | ||
// TODO | ||
return nil | ||
} | ||
cwd, _ := os.Getwd() | ||
|
||
// if no requirements.txt is set we should check to see if we have one in | ||
// dir in provided path or the current working dir, if so we can use that. | ||
if options.RequirementsTXT == "" { | ||
if options.Path != "" { | ||
requirementsTXT = filepath.Join(options.Path, "requirements.txt") | ||
} else { | ||
requirementsTXT = filepath.Join(cwd, "requirements.txt") | ||
} | ||
if requirementsTXT != "" { | ||
fmt.Printf("checking if %s exists... ", requirementsTXT) | ||
if _, err = os.Stat(requirementsTXT); err == nil { | ||
options.RequirementsTXT = requirementsTXT | ||
fmt.Println("yes") | ||
} else { | ||
fmt.Println("no") | ||
} | ||
} | ||
} else { | ||
fmt.Printf("checking if %s exists... ", options.RequirementsTXT) | ||
if _, err = os.Stat(options.RequirementsTXT); err == nil { | ||
fmt.Println("yes") | ||
} else { | ||
fmt.Println("no") | ||
return fmt.Errorf("(%s) does not exist", options.RequirementsTXT) | ||
} | ||
} | ||
|
||
// if requesting virtualenv we need to make sure virtualenv binary exists | ||
if options.VirtualEnv != "" { | ||
virtualenvBinary = checkBinInPath("virtualenv") | ||
if virtualenvBinary == "" { | ||
return fmt.Errorf("unable to find virtualenv in path") | ||
} | ||
} | ||
|
||
// if no virtualenv is set we should check to see if we have a virtualenv | ||
// dir in provided path or the current working dir, if so we can use that. | ||
if options.VirtualEnv == "" { | ||
//fmt.Printf("you specified path %s\n", options.Path) | ||
if options.Path != "" { | ||
virtualEnv = filepath.Join(options.Path, "virtualenv") | ||
options.VirtualEnv = virtualEnv | ||
} else { | ||
virtualEnv = filepath.Join(cwd, "virtualenv") | ||
fmt.Printf("checking if %s exists... ", virtualEnv) | ||
if _, err = os.Stat(virtualEnv); err == nil { | ||
options.VirtualEnv = virtualEnv | ||
fmt.Println("yes") | ||
} else { | ||
fmt.Println("no") | ||
} | ||
} | ||
} | ||
|
||
// if requesting virtualenv we need to make sure virtualenv binary exists | ||
if options.VirtualEnv != "" { | ||
virtualenvBinary = checkBinInPath("virtualenv") | ||
if virtualenvBinary == "" { | ||
return fmt.Errorf("unable to find virtualenv in path") | ||
} | ||
} | ||
|
||
// if a virtualenv is specified make sure that pip is installed | ||
// in it and use that, otherwise make sure its in path. | ||
if options.VirtualEnv != "" { | ||
fmt.Printf("Using VirtualEnv: %s\n", options.VirtualEnv) | ||
pipBinary = filepath.Join(options.VirtualEnv, "bin", "pip") | ||
if _, err = os.Stat(pipBinary); os.IsNotExist(err) { | ||
err = runCmd(virtualenvBinary, []string{options.VirtualEnv}) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
} else { | ||
pipBinary = checkBinInPath("pip") | ||
} | ||
if pipBinary == "" { | ||
return fmt.Errorf("unable to find pip in path") | ||
} | ||
fmt.Printf("Using Pip: %s\n", pipBinary) | ||
|
||
if options.RequirementsTXT != "" { | ||
err = runCmd(pipBinary, []string{"install", "-r", options.RequirementsTXT}) | ||
if err != nil { | ||
return err | ||
} | ||
} else { | ||
err = runCmd(pipBinary, []string{"install", "ansible"}) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
|
||
func installAnsibleFromYum() error { | ||
// TODO | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package cmd | ||
|
||
import ( | ||
//"os/exec" | ||
"github.com/spf13/cobra" | ||
"github.com/paulczar/gosible/ansible" | ||
) | ||
|
||
var installOptions = &ansible.InstallOptions{} | ||
|
||
// runCmd represents the run command | ||
var installCmd = &cobra.Command{ | ||
Use: "install", | ||
Short: "install ansible via pip", | ||
Long: ` | ||
Gosible install will install ansible and/or any dependencies provided. | ||
If you do not specify a virtualenv or requirements file it will attempt | ||
to find them in your local path, or it will try to find them in your | ||
environment if you provide one. | ||
`, | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
err := ansible.InstallViaPip(installOptions) | ||
if err != nil { | ||
return err | ||
} | ||
return nil | ||
}, | ||
} | ||
|
||
func init() { | ||
RootCmd.AddCommand(installCmd) | ||
// stops parsing flags after first unknown flag is found | ||
installCmd.Flags().SetInterspersed(false) | ||
installCmd.Flags().StringVarP(&installOptions.VirtualEnv, "virtualenv", | ||
"v", "", "Path to VirtualEnv to use") | ||
installCmd.Flags().StringVarP(&installOptions.RequirementsTXT, "requirements", | ||
"r", "", "path to requirements.txt") | ||
installCmd.Flags().StringVarP(&installOptions.Path, "environment", | ||
"e", "", "path to your gosible environment") | ||
} |