Skip to content
This repository has been archived by the owner on Aug 9, 2024. It is now read-only.

Commit

Permalink
Add all-zones option for apply and upgrade commands
Browse files Browse the repository at this point in the history
  • Loading branch information
bougou committed Oct 17, 2021
1 parent 38ec0da commit a115fd8
Show file tree
Hide file tree
Showing 32 changed files with 236 additions and 175 deletions.
File renamed without changes.
9 changes: 9 additions & 0 deletions examples/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# using sail to do operations

```bash
$ sail list-components -p foobar

$ sail conf-create -t mytarget -z myzone -p foobar --hosts 127.0.0.1

$ sail apply -t mytarget -z myzone
```
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,18 @@ command_warnings = False
# ansible-playbook will automatically insert `${playbook-yaml-dir}/roles` at the begining and `${playbook-yaml-dir}` at the end
# {playbook-yaml-dir}/roles
# {playbook-yaml-dir}
# exclude other ansible common role paths
# roles_path = shared_roles:~/.ansible/roles:/usr/share/ansible/roles:/etc/ansible/roles
roles_path = shared_roles
# relative to the directory holding ansible.cfg

# For a specific product, the product dir is `products/<productName>`.
# We put all playbooks under it, and create a `roles` direcotry.

# The above configuration will make sail ansible to search roles under the following directories and ober the order.
# <sailProductsDir>/<productName>/roles:<sailProductsDir>/shared_roles:<sailProductsDir>/<productName>

#roles_path = shared_roles:~/.ansible/roles:/usr/share/ansible/roles:/etc/ansible/roles
[privilege_escalation]
[paramiko_connection]
[ssh_connection]
Expand Down
60 changes: 60 additions & 0 deletions examples/products/foobar/.sail.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
- name: foobar-api
hosts: "{{ _ansiblepattern_foobar_api | default('foobar-api') }}"
any_errors_fatal: false
gather_facts: true
become: false
roles:
- role: foobar-api
tags:
- foobar-api
- foobar-api-foobar-api
tags:
- play-foobar-api
- name: foobar-backend
hosts: "{{ _ansiblepattern_foobar_backend | default('foobar-backend') }}"
any_errors_fatal: false
gather_facts: true
become: false
roles:
- role: foobar-backend
tags:
- foobar-backend
- foobar-backend-foobar-backend
tags:
- play-foobar-backend
- name: foobar-cache
hosts: "{{ _ansiblepattern_foobar_cache | default('foobar-cache') }}"
any_errors_fatal: false
gather_facts: true
become: false
roles:
- role: foobar-cache
tags:
- foobar-cache
- foobar-cache-foobar-cache
tags:
- play-foobar-cache
- name: foobar-db
hosts: "{{ _ansiblepattern_foobar_db | default('foobar-db') }}"
any_errors_fatal: false
gather_facts: true
become: false
roles:
- role: foobar-db
tags:
- foobar-db
- foobar-db-foobar-db
tags:
- play-foobar-db
- name: foobar-web
hosts: "{{ _ansiblepattern_foobar_web | default('foobar-web') }}"
any_errors_fatal: false
gather_facts: true
become: false
roles:
- role: foobar-web
tags:
- foobar-web
- foobar-web-foobar-web
tags:
- play-foobar-web
5 changes: 5 additions & 0 deletions examples/products/foobar/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# foobar

Hello, this is foobar.

You can use `sail` to deploy this product.
49 changes: 49 additions & 0 deletions examples/products/foobar/components.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
foobar-web:
version: "v0.0.1"
enabled: true
external: false
form: "server"
services:
http-web:
scheme: "http"
port: 80
https-web:
scheme: "https"
port: 443

foobar-api:
version: "v0.0.2"
enabled: true
external: false
form: "server"
services:
default:
scheme: http
port: 8080

foobar-backend:
version: "v0.0.3"
enabled: true
external: false
form: "server"

foobar-db:
version: "v0.0.4"
enabled: false
external: true
form: "server"
services:
default:
scheme: "tcp"
host: "mysql-xyz.some-cloud.com"
port: 3306

foobar-cache:
version: "v0.0.5"
enabled: true
external: false
form: "server"
services:
default:
scheme: "tcp"
port: 6309
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
---
- name: real task
- name: tasks for foobar-api
debug:
msg: install/config/start foobar-api
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
---
- name: real task
- name: tasks for foobar-backend
debug:
msg: install/config/start foobar-backend
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
---
- name: real task
- name: tasks for foobar-cache
debug:
msg: install/config/start foobar-cache
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
---
- name: real task
- name: tasks for foobar-db
debug:
msg: install/config/start foobar-db
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
---
- name: real task
- name: tasks for foobar-web
debug:
msg: install/config/start foobar-web
Empty file.
3 changes: 3 additions & 0 deletions examples/products/foobar/vars.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
installDir: /opt
dataDir: /data
timezone: "Asia/Shanghai"
15 changes: 6 additions & 9 deletions pkg/ansible/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@ type Group struct {
}
```

1. Inventory 虽然是一个 struct,但本质上是一个里面的 items map
2. Group 是一个 struct,
## Inventory Content

```json
{
Expand All @@ -33,7 +32,7 @@ type Group struct {
"192.168.28.72"
],
"vars": {
"ansible_ssh_user": "johndoe",
"ansible_ssh_user": "someuser",
"ansible_ssh_private_key_file": "~/.ssh/mykey",
"example_variable": "value"
},
Expand All @@ -44,7 +43,7 @@ type Group struct {
"192.168.28.72"
],
"vars": {
"ansible_ssh_user": "johndoe",
"ansible_ssh_user": "someuser",
"ansible_ssh_private_key_file": "~/.ssh/mykey",
"example_variable": "value"
},
Expand All @@ -58,7 +57,7 @@ type Group struct {
"192.168.28.72"
],
"vars": {
"ansible_ssh_user": "johndoe",
"ansible_ssh_user": "someuser",
"ansible_ssh_private_key_file": "~/.ssh/mykey",
"example_variable": "value"
}
Expand All @@ -76,7 +75,5 @@ type Group struct {
}
```

以上面的 JSON 内容为例:
1. 整个 JSON 可以映射到一个 Inventory 结构体中(实际上是 inventory 结构体 items 字段里面, items 是一个 map)
2. Inventory 的 items map 里面 key 表示 group Name,value 表示一个 group,可以映射到一个 Group 结构体中。
3. Group 里面有 Hosts, Vars, Children 字段。
- The above JSON content can be decoded into a `Inventory` struct, actually the `items` map of `Inventory`.
- The key of `items` map represents a group name, and the value can be decoded into `Group` struct.
2 changes: 1 addition & 1 deletion pkg/ansible/inventory.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func (i *Inventory) MarshalJSON() ([]byte, error) {

func (i *Inventory) UnmarshalJSON(data []byte) error {
var m GroupsMap = make(map[string]*Group)
decoder := json.NewDecoder(bytes.NewBuffer((data)))
decoder := json.NewDecoder(bytes.NewBuffer(data))
// decoder.UseNumber()
if err := decoder.Decode(&m); err != nil {
return err
Expand Down
38 changes: 36 additions & 2 deletions pkg/commands/apply/apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ func NewCmdApply(sailOption *models.SailOption) *cobra.Command {

cmd.Flags().StringVarP(&o.TargetName, "target", "t", o.TargetName, "target name")
cmd.Flags().StringVarP(&o.ZoneName, "zone", "z", o.ZoneName, "zone name")
cmd.Flags().BoolVarP(&o.AllZones, "all-zones", "", o.AllZones, "choose all zones, no meaning if explicitly specified a zone")
cmd.Flags().StringVarP(&o.Playbook, "playbook", "p", "", "optional playbook name")
cmd.Flags().StringVarP(&o.StartAtPlay, "start-at-play", "", "", "start the playbook from the play with this tag name")
cmd.Flags().StringArrayVarP(&o.Components, "component", "c", o.Components, "the component")
Expand All @@ -40,6 +41,7 @@ func NewCmdApply(sailOption *models.SailOption) *cobra.Command {
type ApplyOptions struct {
TargetName string `json:"target_name"`
ZoneName string `json:"zone_name"`
AllZones bool `json:"all_zones"`
Playbook string `json:"playbook"`

StartAtPlay string `json:"start_at_playbook"`
Expand Down Expand Up @@ -69,13 +71,45 @@ func (o *ApplyOptions) Complete(cmd *cobra.Command, args []string) error {
}

func (o *ApplyOptions) Validate() error {
if o.TargetName == "" {
return errors.New("must specify target name")
}
if o.ZoneName == "" && !o.AllZones {
return errors.New("must specify zone name, or choose all zones")
}
return nil
}

func (o *ApplyOptions) Run(args []string) error {
options.PrintColorHeader(o.TargetName, o.ZoneName)
if o.ZoneName != "" {
return o.run(o.TargetName, o.ZoneName, args)
}

if o.AllZones {
t := target.NewTarget(o.sailOption, o.TargetName)
zoneNames, err := t.AllZones()
if err != nil {
return fmt.Errorf("determine all zones for target (%s) failed, err: %s", o.TargetName, err)
}

for i, zoneName := range zoneNames {
if i != 0 {
fmt.Printf("\n\n\n")
}
if err := o.run(o.TargetName, zoneName, args); err != nil {
// todo, add options for error handling
continue
}
}
}

return nil
}

func (o *ApplyOptions) run(targetName string, zoneName string, args []string) error {
options.PrintColorHeader(targetName, zoneName)

zone := target.NewZone(o.sailOption, o.TargetName, o.ZoneName)
zone := target.NewZone(o.sailOption, targetName, zoneName)
if err := zone.Load(); err != nil {
return err
}
Expand Down
29 changes: 27 additions & 2 deletions pkg/commands/upgrade/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ func NewCmdUpgrade(sailOption *models.SailOption) *cobra.Command {

cmd.Flags().StringVarP(&o.TargetName, "target", "t", o.TargetName, "target name")
cmd.Flags().StringVarP(&o.ZoneName, "zone", "z", o.ZoneName, "zone name")
cmd.Flags().BoolVarP(&o.AllZones, "all-zones", "", o.AllZones, "choose all zones, no meaning if explicitly specified a zone")
cmd.Flags().StringArrayVarP(&o.Components, "component", "c", o.Components, "the component")
cmd.Flags().BoolVarP(&o.Ansible, "ansible", "", o.Ansible, "choose components deployed as server")
cmd.Flags().BoolVarP(&o.Helm, "helm", "", o.Helm, "choose components deployed as pod")
Expand All @@ -36,6 +37,7 @@ func NewCmdUpgrade(sailOption *models.SailOption) *cobra.Command {
type UpgradeOptions struct {
TargetName string `json:"target_name"`
ZoneName string `json:"zone_name"`
AllZones bool `json:"all_zones"`

Components []string `json:"component"`
Ansible bool `json:"ansible"`
Expand Down Expand Up @@ -66,13 +68,36 @@ func (o *UpgradeOptions) Validate() error {
if o.TargetName == "" {
return errors.New("must specify target name")
}
if o.ZoneName == "" {
return errors.New("must specify zone name")
if o.ZoneName == "" && !o.AllZones {
return errors.New("must specify zone name, or choose all zones")
}
return nil
}

func (o *UpgradeOptions) Run(args []string) error {
if o.ZoneName != "" {
return o.run(o.TargetName, o.ZoneName, args)
}

if o.AllZones {
t := target.NewTarget(o.sailOption, o.TargetName)
zoneNames, err := t.AllZones()
if err != nil {
return fmt.Errorf("determine all zones for target (%s) failed, err: %s", o.TargetName, err)
}

for _, zoneName := range zoneNames {
if err := o.run(o.TargetName, zoneName, args); err != nil {
// todo, add options for error handling
continue
}
}
}

return nil
}

func (o *UpgradeOptions) run(targetName string, zoneName string, args []string) error {
options.PrintColorHeader(o.TargetName, o.ZoneName)

zone := target.NewZone(o.sailOption, o.TargetName, o.ZoneName)
Expand Down
File renamed without changes.
10 changes: 8 additions & 2 deletions pkg/models/target/ansible.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,17 @@ gather_subset=!hardware
command_warnings = False

# ansible-playbook will automatically insert `${playbook-yaml-dir}/roles` at the begining and `${playbook-yaml-dir}` at the end
# {playbook-yaml-dir}/roles, So we need to put all playbook file under <sailProductsDir>/<productName>/ directory.
# {playbook-yaml-dir}/roles
# {playbook-yaml-dir}
# exclude other ansible common role paths
# roles_path = shared_roles:~/.ansible/roles:/usr/share/ansible/roles:/etc/ansible/roles
roles_path = shared_roles
# relative to the directory holding ansible.cfg
# the above configuration will make ansible to search roles under the following directories and ober the order.

# For a specific product, the product dir is `products/<productName>`.
# We put all playbooks under it, and create a `roles` direcotry.

# The above configuration will make sail ansible to search roles under the following directories and ober the order.
# <sailProductsDir>/<productName>/roles:<sailProductsDir>/shared_roles:<sailProductsDir>/<productName>

#roles_path = shared_roles:~/.ansible/roles:/usr/share/ansible/roles:/etc/ansible/roles
Expand Down
Loading

0 comments on commit a115fd8

Please sign in to comment.