-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #12 from APoniatowski/Dev
Dev -> v1.0.0
- Loading branch information
Showing
7 changed files
with
182 additions
and
62 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 |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package clioptions | ||
|
||
import ( | ||
"bufio" | ||
"os" | ||
"strconv" | ||
"strings" | ||
) | ||
|
||
// GeneralCommandParse CLI parser for general commands to linux servers | ||
func GeneralCommandParse(cmd []string) string { | ||
command := strconv.Quote(strings.Join(cmd, " ")) | ||
command = "sh -c " + command + " 2>&1" | ||
return command | ||
} | ||
|
||
//BashScriptParse bash script parser, to pass write the script on the server, run it and remove it. | ||
// It also accepts args for the script. Dependency scripts will not work, as they are considered a separate script | ||
func BashScriptParse(cmd string, cmdargs []string) string { | ||
scriptargs := strings.Join(cmdargs, " ") | ||
script, _ := os.Open(cmd) | ||
defer script.Close() | ||
scanner := bufio.NewScanner(script) | ||
scanner.Split(bufio.ScanLines) | ||
var lines []string | ||
for scanner.Scan() { | ||
lines = append(lines, scanner.Text()) | ||
lines = append(lines, "\n") | ||
} | ||
parsedcmd := strconv.Quote(strings.Join(lines, "")) | ||
parsedcmd = strings.Replace(parsedcmd, `$`, `\$`, -1) | ||
parsedlines := "set +H;echo -e " + parsedcmd + " > /tmp/gossh-script.sh;bash /tmp/gossh-script.sh " + scriptargs + ";rm /tmp/gossh-script.sh;set -H" | ||
return parsedlines | ||
} |
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,30 +1,26 @@ | ||
ServerGroup1: | ||
Server11: | ||
FQDN: hostname11.whatever.com | ||
Username: user11 | ||
Password: password11 | ||
Key_Path: /path/to/key | ||
Port: 22 | ||
Need_Sudo: false | ||
ServerGroup1: #group name, spacing does not matter | ||
Server11: #server name, spacing does not matter | ||
FQDN: hostname11.whatever.com # | ||
Username: user11 ## | ||
Password: password11 ### FQDN, is needed. Username defaults to root, | ||
Key_Path: /path/to/key ## password or key needed, ports default to 22 | ||
Port: 22 # | ||
Server12: | ||
FQDN: hostname12.whatever.com | ||
Username: user12 | ||
Password: password12 | ||
Key_Path: /path/to/key | ||
Port: 223 | ||
Need_Sudo: true | ||
ServerGroup2: | ||
Server21: | ||
FQDN: hostname21.whatever.com | ||
Username: user21 | ||
Password: password21 | ||
Key_Path: /path/to/key | ||
Port: 2233 | ||
Need_Sudo: false | ||
Server22: | ||
FQDN: hostname22.whatever.com | ||
Username: user22 | ||
Password: password22 | ||
Key_Path: /path/to/key | ||
Port: | ||
Need_Sudo: true |
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,50 +1,146 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
"os" | ||
"strconv" | ||
"strings" | ||
|
||
"github.com/APoniatowski/GoSSH/clioptions" | ||
"github.com/APoniatowski/GoSSH/sshlib" | ||
"github.com/APoniatowski/GoSSH/yamlparser" | ||
"github.com/urfave/cli" | ||
) | ||
|
||
// Error checking function | ||
func generalError(e error) { | ||
if e != nil { | ||
log.Fatal(e) | ||
} | ||
} | ||
|
||
// Main function to carry out operations | ||
func main() { | ||
var cmd []string | ||
if len(os.Args) > 2 { | ||
cmd = os.Args[2:] // will change this to 3 later, when I see a need to expand on more arguments, eg. running only 1 group, or x amount of servers | ||
} else { | ||
fmt.Println("No command was specified, please specify a command.") | ||
os.Exit(1) | ||
|
||
app := cli.NewApp() | ||
app.Name = "GoSSH" | ||
app.Version = "1.0.0" | ||
app.Usage = "Open Source Go Infrastucture Automation Tool" | ||
app.UsageText = "GoSSH [global options] command [subcommand] [script or arguments...]" | ||
app.EnableBashCompletion = true | ||
app.Commands = []cli.Command{ | ||
{ | ||
Name: "sequential", | ||
Aliases: []string{"s"}, | ||
Usage: "Run the command sequentially on all servers in your config file", | ||
Action: func(c *cli.Context) error { | ||
yamlparser.Rollcall() | ||
cmd = os.Args[2:] | ||
command := clioptions.GeneralCommandParse(cmd) | ||
sshlib.RunSequentially(&yamlparser.Config, &command) | ||
return nil | ||
}, | ||
Subcommands: []cli.Command{ | ||
{ | ||
Name: "run", | ||
Usage: "Run a bash script on the defined servers", | ||
Action: func(c *cli.Context) error { | ||
yamlparser.Rollcall() | ||
cmd := os.Args[3] | ||
cmdargs := os.Args[4:] | ||
command := clioptions.BashScriptParse(cmd, cmdargs) | ||
sshlib.RunAllServers(&yamlparser.Config, &command) | ||
return nil | ||
}, | ||
}, | ||
//-----------------placeholder-------------------- | ||
// { | ||
// Name: "remove", | ||
// Usage: "remove an existing template", | ||
// Action: func(c *cli.Context) error { | ||
// fmt.Println("removed task template: ", c.Args().First()) | ||
// return nil | ||
// }, | ||
// }, | ||
//-----------------placeholder-------------------- | ||
}, | ||
}, | ||
{ | ||
Name: "groups", | ||
Aliases: []string{"g"}, | ||
Usage: "Run the command on all servers per group concurrently in your config file", | ||
Action: func(c *cli.Context) error { | ||
yamlparser.Rollcall() | ||
cmd = os.Args[2:] | ||
command := clioptions.GeneralCommandParse(cmd) | ||
sshlib.RunGroups(&yamlparser.Config, &command) | ||
return nil | ||
}, | ||
Subcommands: []cli.Command{ | ||
{ | ||
Name: "run", | ||
Usage: "Run a bash script on the defined servers", | ||
Action: func(c *cli.Context) error { | ||
yamlparser.Rollcall() | ||
cmd := os.Args[3] | ||
cmdargs := os.Args[4:] | ||
command := clioptions.BashScriptParse(cmd, cmdargs) | ||
sshlib.RunAllServers(&yamlparser.Config, &command) | ||
return nil | ||
}, | ||
}, | ||
//-----------------placeholder-------------------- | ||
// { | ||
// Name: "remove", | ||
// Usage: "remove an existing template", | ||
// Action: func(c *cli.Context) error { | ||
// fmt.Println("removed task template: ", c.Args().First()) | ||
// return nil | ||
// }, | ||
// }, | ||
//-----------------placeholder-------------------- | ||
}, | ||
}, | ||
{ | ||
Name: "all", | ||
Aliases: []string{"a"}, | ||
Usage: "Run the command on all servers concurrently in your config file", | ||
Action: func(c *cli.Context) error { | ||
yamlparser.Rollcall() | ||
cmd = os.Args[2:] | ||
command := clioptions.GeneralCommandParse(cmd) | ||
sshlib.RunAllServers(&yamlparser.Config, &command) | ||
return nil | ||
}, | ||
Subcommands: []cli.Command{ | ||
{ | ||
Name: "run", | ||
Usage: "Run a bash script on the defined servers", | ||
Action: func(c *cli.Context) error { | ||
yamlparser.Rollcall() | ||
cmd := os.Args[3] | ||
cmdargs := os.Args[4:] | ||
command := clioptions.BashScriptParse(cmd, cmdargs) | ||
sshlib.RunAllServers(&yamlparser.Config, &command) | ||
return nil | ||
}, | ||
}, | ||
//-----------------placeholder-------------------- | ||
// { | ||
// Name: "remove", | ||
// Usage: "remove an existing template", | ||
// Action: func(c *cli.Context) error { | ||
// fmt.Println("removed task template: ", c.Args().First()) | ||
// return nil | ||
// }, | ||
// }, | ||
//-----------------placeholder-------------------- | ||
}, | ||
}, | ||
} | ||
|
||
command := strconv.Quote(strings.Join(cmd, " ")) | ||
command = "sh -c " + command + " 2>&1" | ||
yamlparser.Rollcall() | ||
// app.Flags = []cli.Flag{ | ||
// cli.StringFlag{ | ||
// Name: "lang, l", | ||
// Value: "english", | ||
// Usage: "language for the greeting", | ||
// }, | ||
// } | ||
|
||
switch options := os.Args[1]; options { | ||
case "seq": | ||
sshlib.RunSequentially(&yamlparser.Config, &command) | ||
case "groups": | ||
sshlib.RunGroups(&yamlparser.Config, &command) | ||
case "all": | ||
sshlib.RunAllServers(&yamlparser.Config, &command) | ||
default: | ||
fmt.Println("Usage: gossh [option] [command]") | ||
fmt.Println("Options:") | ||
fmt.Println(" seq - Run the command sequentially on all servers in your config file") | ||
fmt.Println(" groups - Run the command on all servers per group concurrently in your config file") | ||
fmt.Println(" all - Run the command on all servers concurrently in your config file") | ||
err := app.Run(os.Args) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
} |
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