Skip to content

Commit

Permalink
add subcommand site, remove subcommand add
Browse files Browse the repository at this point in the history
  • Loading branch information
wokamoto committed Jul 25, 2017
1 parent 90244aa commit e857a25
Show file tree
Hide file tree
Showing 4 changed files with 263 additions and 60 deletions.
2 changes: 1 addition & 1 deletion .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ root = true

[*]
indent_style = space
indent_size = 2
indent_size = 4
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
Expand Down
11 changes: 10 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,16 @@ curl -L -s https://github.com/amimoto-ami/amimoto-cli/raw/master/install.sh | su
`sudo amimoto cache --purge`

##### Add virtual host example.com
`sudo amimoto add example.com`
`sudo amimoto site --add example.com`

##### Disable virtual host example.com
`sudo amimoto site --disable example.com`

##### Enable virtual host example.com
`sudo amimoto site --enable example.com`

##### Remove virtual host example.com
`sudo amimoto site --remove example.com`

### Developing New Features

Expand Down
Binary file modified amimoto
Binary file not shown.
310 changes: 252 additions & 58 deletions amimoto.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import (
"path/filepath"
"encoding/json"
"github.com/koron/go-dproxy"
"database/sql"
_ "github.com/go-sql-driver/mysql"
)

type dbConf struct {
Expand All @@ -22,9 +24,11 @@ type dbConf struct {
func main() {
optarg.Header("Sub command cache")
optarg.Add("p", "purge", "Clear NGINX proxy cache", false)
optarg.Header("Sub command add {site name}")
//optarg.Add("s", "site", "Adds site with WordPress files and DB creation", "")

optarg.Header("Sub command site")
optarg.Add("a", "add", "Add site (string)", "")
optarg.Add("e", "enable", "Enable site (string)", "")
optarg.Add("d", "disable", "Disable site (string)", "")
optarg.Add("r", "remove", "Remove site (string)", "")
ch := optarg.Parse()
<-ch

Expand All @@ -33,20 +37,98 @@ func main() {
switch cmd {
case "cache":
cache()
case "add":
if len(optarg.Remainder) > 1 {
add(optarg.Remainder[1])
}
case "site":
site()
default:
optarg.Usage()
}
} else {
optarg.Usage()
}
}

func cache() {
var purge bool
var dir string
dir = "/var/cache/nginx/proxy_cache/"
for opt := range optarg.Parse() {
switch opt.ShortName {
case "p":
purge = opt.Bool()
}
}
paths := dirwalk(dir)
fmt.Println("Cached URLs: ", len(paths))
if purge {
files, err := ioutil.ReadDir(dir)
if err != nil {
panic(err)
}

for _, file := range files {
err := os.RemoveAll(filepath.Join(dir, file.Name()))
if err != nil {
panic(err)
}
}
fmt.Println("Cache purge successfull!")
}
}

func dirwalk(dir string) []string {
files, err := ioutil.ReadDir(dir)
if err != nil {
panic(err)
}

var paths []string
for _, file := range files {
if file.IsDir() {
paths = append(paths, dirwalk(filepath.Join(dir, file.Name()))...)
continue
}
paths = append(paths, filepath.Join(dir, file.Name()))
}

return paths
}

func site() {
var site string
for opt := range optarg.Parse() {
site = opt.String()
if site != "" {
switch opt.ShortName {
case "a":
fmt.Println("Site add: ", site)
add(site)
case "d":
fmt.Println("Site disable: ", site)
disable(site)
case "e":
fmt.Println("Site enableL: ", site)
enable(site)
case "r":
fmt.Println("Site remove: ", site)
remove(site)
default:
optarg.Usage()
}
} else {
optarg.Usage()
}
}
}

func Exists(name string) bool {
_, err := os.Stat(name)
return !os.IsNotExist(err)
}

func add(site string) {
fmt.Println("Site URL: ", site)
setupCmd := "/usr/local/bin/wp-setup"

// wp-setup
_, lookErr := exec.LookPath(setupCmd)
if lookErr != nil {
panic(lookErr)
Expand All @@ -66,80 +148,192 @@ func add(site string) {
}
cmd.Wait()

db, err := readAmimotoDBConf(site)
if err != nil {
panic(err)
if Exists("/etc/nginx/conf.d/"+site+".conf.disable") {
enable(site)
}

// read DB Configuration and show
dbConf, readErr := readAmimotoDBConf(site)
if readErr != nil {
panic(readErr)
}
fmt.Println()
fmt.Println("DB Host: ", db.host)
fmt.Println("DB Name: ", db.name)
fmt.Println("DB User: ", db.user)
fmt.Println("DB Pass: ", db.pass)
fmt.Println("DB Host: ", dbConf.host)
fmt.Println("DB Name: ", dbConf.name)
fmt.Println("DB User: ", dbConf.user)
fmt.Println("DB Pass: ", dbConf.pass)
}

func readAmimotoDBConf(site string) (dbConf, error) {
var c interface{}

filename := "/opt/local/" + site + ".json"
jsonString, err := ioutil.ReadFile(filename)
if err != nil {
return dbConf{}, err
func nginxConfRename(conf string, enable bool) error {
var err error
confDir := "/etc/nginx/conf.d/"
if enable {
if Exists(filepath.Join(confDir,conf+".disable")) {
err = os.Rename(filepath.Join(confDir,conf+".disable"), filepath.Join(confDir,conf))
return err
}
} else {
if Exists(filepath.Join(confDir,conf)) {
err = os.Rename(filepath.Join(confDir,conf), filepath.Join(confDir,conf+".disable"))
return err
}
}
err = json.Unmarshal(jsonString, &c)
return nil
}

func nginxReload() error {
cmd := exec.Command("/sbin/service", "nginx", "reload")
stdout, err := cmd.StdoutPipe()
if err != nil {
return dbConf{}, err
return err
}
db_name, _ := dproxy.New(c).M("wordpress").M("db").M("db_name").String()
db_user, _ := dproxy.New(c).M("wordpress").M("db").M("user_name").String()
db_pass, _ := dproxy.New(c).M("wordpress").M("db").M("password").String()
db_host, _ := dproxy.New(c).M("wordpress").M("db").M("host").String()
conf := dbConf{db_name, db_user, db_pass, db_host}

return conf, nil
cmd.Start()
scanner := bufio.NewScanner(stdout)
for scanner.Scan() {
fmt.Println(scanner.Text())
}
cmd.Wait()
return nil
}

func cache() {
var purge bool
var dir string
dir = "/var/cache/nginx/proxy_cache/"
for opt := range optarg.Parse() {
switch opt.ShortName {
case "p":
purge = opt.Bool()
func disable(site string) {
var err error

if Exists("/etc/nginx/conf.d/"+site+".conf") {
// rename nginx conf files
confs := map[string]string{
"front": site+".conf",
"ssl": site+"-ssl.conf",
"backend": site+".backend.conf",
}
}
paths := dirwalk(dir)
fmt.Println("Cached URLs: ", len(paths))
if purge {
files, err := ioutil.ReadDir(dir)
for _, filename := range confs {
err = nginxConfRename(filename, false)
if err != nil {
panic(err)
}
}

// nginx conf reload
err = nginxReload()
if err != nil {
panic(err)
}

for _, file := range files {
err := os.RemoveAll(filepath.Join(dir, file.Name()))
fmt.Println("Site (" + site + ") disabled!")

} else {
fmt.Println("Site (" + site + ") config not found.")
os.Exit(1)
}
}

func enable(site string) {
var err error

if Exists("/etc/nginx/conf.d/"+site+".conf.disable") {
// rename nginx conf files
confs := map[string]string{
"front": site+".conf",
"ssl": site+"-ssl.conf",
"backend": site+".backend.conf",
}
for _, filename := range confs {
err = nginxConfRename(filename, true)
if err != nil {
panic(err)
}
}
fmt.Println("Cache purge successfull!")

// nginx conf reload
err = nginxReload()
if err != nil {
panic(err)
}

fmt.Println("Site " + site + " enabled!")

} else {
fmt.Println("Site (" + site + ") config not found.")
os.Exit(1)
}
}

func dirwalk(dir string) []string {
files, err := ioutil.ReadDir(dir)
if err != nil {
panic(err)
}
func remove(site string) {
if Exists(filepath.Join("/opt/local/", site+".json")) {
// get DB Connect info
dbConf, readErr := readAmimotoDBConf(site)
if readErr != nil {
panic(readErr)
}

var paths []string
for _, file := range files {
if file.IsDir() {
paths = append(paths, dirwalk(filepath.Join(dir, file.Name()))...)
continue
//drop database
db, dbErr := sql.Open("mysql", dbConf.user+":"+dbConf.pass+"@/"+dbConf.name)
if dbErr != nil {
panic(dbErr)
}
paths = append(paths, filepath.Join(dir, file.Name()))
defer db.Close()
_, queryErr := db.Query("DROP DATABASE "+dbConf.name)
if queryErr != nil {
panic(queryErr.Error())
}

// remove site.conf files
var err error
dir := "/etc/nginx/conf.d/"
confs := map[string]string{
"front": filepath.Join(dir, site+".conf"),
"ssl": filepath.Join(dir, site+"-ssl.conf"),
"backend": filepath.Join(dir, site+".backend.conf"),
"db_json": filepath.Join("/opt/local/", site+".json"),
"db_sql": filepath.Join("/opt/local/", "createdb-"+site+".sql"),
}
for _, filename := range confs {
if Exists(filename) {
err = os.Remove(filename)
if err != nil {
panic(err)
}
}
if Exists(filename+".disable") {
err = os.Remove(filename+".disable")
if err != nil {
panic(err)
}
}
}
err = os.RemoveAll(filepath.Join("/var/www/vhosts/", site))

// nginx conf reload
err = nginxReload()
if err != nil {
panic(err)
}

fmt.Println("Site " + site + " removed!")
}
}

return paths
func readAmimotoDBConf(site string) (dbConf, error) {
filename := "/opt/local/" + site + ".json"
if Exists(filename) {
var c interface{}
jsonString, err := ioutil.ReadFile(filename)
if err != nil {
return dbConf{}, err
}
err = json.Unmarshal(jsonString, &c)
if err != nil {
return dbConf{}, err
}
db_name, _ := dproxy.New(c).M("wordpress").M("db").M("db_name").String()
db_user, _ := dproxy.New(c).M("wordpress").M("db").M("user_name").String()
db_pass, _ := dproxy.New(c).M("wordpress").M("db").M("password").String()
db_host, _ := dproxy.New(c).M("wordpress").M("db").M("host").String()
conf := dbConf{db_name, db_user, db_pass, db_host}
return conf, nil

} else {
return dbConf{}, nil
}
}

0 comments on commit e857a25

Please sign in to comment.