Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[wip] feat: switch edition command #1381

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions lib/ce.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# encoding: utf-8
from __future__ import unicode_literals

from .service import Service
from .service import PrimaryMasterService


def add_command(subparsers):
Service(subparsers, 'ce')
PrimaryMasterService(subparsers, 'ce')
6 changes: 6 additions & 0 deletions lib/consts.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,9 @@
ENV_K3S = "K3S"
ENV_K8S_V115 = "K8S_V115"
ENV_VAL_TRUE = 'TRUE'

EDITION_CE= 'ce'
EDITION_CE_SUPPORT = 'ce/support'
EDITION_EE = 'ee'

EDITIONS = [EDITION_CE, EDITION_CE_SUPPORT]
4 changes: 3 additions & 1 deletion lib/ocboot.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
KEY_AS_HOST_ON_VM = 'as_host_on_vm'
KEY_EXTRA_PACKAGES = 'extra_packages'
KEY_IMAGE_REPOSITORY = 'image_repository'
KEY_K8S_CONTROLPLANE_HOST = 'k8s_controlplane_host'

KEY_K8S_OR_K3S = 'env_k8s_or_k3s'
KEY_K3S_API_ENDPOINT = "api_endpoint"
Expand All @@ -44,6 +45,7 @@
KEY_STACK_EDGE = 'Edge'
KEY_STACK_CMP = 'CMP'
KEY_STACK_LIST = [KEY_STACK_FULLSTACK, KEY_STACK_EDGE, KEY_STACK_CMP]
KEY_TARGET_EDITION = 'TARGET_EDITION'

KEY_USER_DNS = 'user_dns'

Expand Down Expand Up @@ -423,7 +425,7 @@ def ansible_vars(self):
vars = {
'docker_registry_mirrors': self.registry_mirrors,
'docker_insecure_registries': self.insecure_registries,
'k8s_controlplane_host': self.controlplane_host,
KEY_K8S_CONTROLPLANE_HOST: self.controlplane_host,
KEY_K3S_API_ENDPOINT: self.controlplane_host,
'k8s_controlplane_port': self.controlplane_port,
KEY_K3S_API_PORT: self.controlplane_port,
Expand Down
7 changes: 6 additions & 1 deletion lib/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,16 @@ def inject_ssh_hosts_options(parser):
return parser


def inject_add_nodes_options(parser):
def inject_primary_node_options(parser):
parser.add_argument("primary_master_host",
metavar="FIRST_MASTER_HOST",
help="onecloud cluster primary master host, \
e.g., 10.1.2.56")


def inject_add_nodes_options(parser):
inject_primary_node_options(parser)

parser.add_argument("target_node_hosts",
nargs='+',
default=[],
Expand Down Expand Up @@ -90,6 +94,7 @@ def inject_add_nodes_runtime_options(parser):
choices=[consts.RUNTIME_QEMU, consts.RUNTIME_CONTAINERD],
help="select runtime type when adding node. default: qemu")


def inject_auto_backup_options(parser):
parser.add_argument(
'--backup-path', help="backup path, default: /opt/yunion/backup", default="/opt/yunion/backup")
Expand Down
40 changes: 37 additions & 3 deletions lib/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@

from __future__ import unicode_literals

from .ocboot import KEY_ENABLE_CONTAINERD, KEY_IMAGE_REPOSITORY, NodeConfig, Config
from .ocboot import KEY_ENABLE_CONTAINERD, KEY_IMAGE_REPOSITORY, KEY_K8S_CONTROLPLANE_HOST, NodeConfig, Config
from .cmd import run_ansible_playbook
from .ansible import get_inventory_config
from .parser import inject_add_hostagent_options
from .parser import inject_add_hostagent_options, inject_primary_node_options, inject_ssh_options
from .parser import inject_add_nodes_options
from .parser import inject_auto_backup_options
from .parser import inject_ssh_hosts_options
Expand Down Expand Up @@ -67,7 +67,7 @@ def __init__(self, target_nodes, ssh_user, ssh_private_file, ssh_port):
}
self.nodes_config = NodeConfig(Config(conf_dict))

def run(self, action):
def run(self, action, vars=None):
inventory_content = get_inventory_config(self.nodes_config)
yaml_content = utils.to_yaml(inventory_content)
filepath = '/tmp/cluster_%s_node_services_inventory.yml' % action
Expand All @@ -77,7 +77,41 @@ def run(self, action):
run_ansible_playbook(
filepath,
'./onecloud/%s-services.yml' % action,
vars=vars,
)


class PrimaryMasterService(BaseService):

def __init__(self, subparsers, action):
super().__init__(subparsers, action, f"{action} service of primary_master_host")

def inject_options(self, parser):
inject_primary_node_options(parser)
inject_ssh_options(parser)

def get_ansible_vars(self, args, cluster, primary_master_host):
vars = get_ansible_global_vars(
cluster.get_current_version(),
cluster.is_using_k3s())
vars[KEY_K8S_CONTROLPLANE_HOST] = primary_master_host
return vars


def do_action(self, args):
cluster = construct_cluster(
args.primary_master_host,
args.ssh_user,
args.ssh_private_file,
args.ssh_port)
vars = self.get_ansible_vars(args, cluster, args.primary_master_host)
config = NodesConfig(
[args.primary_master_host],
args.ssh_user,
args.ssh_private_file,
args.ssh_port,
)
config.run(self.action, vars=vars)


class AddNodeBaseService(BaseService):
Expand Down
29 changes: 29 additions & 0 deletions lib/switch_edition.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# encoding: utf-8
from __future__ import unicode_literals

from lib import consts
from lib.ocboot import KEY_TARGET_EDITION

from .service import PrimaryMasterService


def add_command(subparsers):
SwitchEditionService(subparsers)


class SwitchEditionService(PrimaryMasterService):

def __init__(self, subparsers):
super().__init__(subparsers, 'switch-edition')

def inject_options(self, parser):
super().inject_options(parser)
parser.add_argument('edition',
metavar='EDITION',
choices=consts.EDITIONS,
help=f"choice edition from {consts.EDITIONS}")

def get_ansible_vars(self, args, cluster, primary_master_host):
vars = super().get_ansible_vars(args, cluster, primary_master_host)
vars[KEY_TARGET_EDITION] = args.edition
return vars
5 changes: 2 additions & 3 deletions ocboot.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import sys
import argparse

from lib import install
from lib import install, switch_edition
from lib import upgrade
from lib import backup, restore
from lib import add_node
Expand All @@ -23,8 +23,7 @@ def main():
add_node.add_command(subparsers)
auto_backup.add_command(subparsers)
backup.add_command(subparsers)
ce.add_command(subparsers)
ee.add_command(subparsers)
switch_edition.add_command(subparsers)
install.add_command(subparsers)
restore.add_command(subparsers)
start.add_command(subparsers)
Expand Down
2 changes: 1 addition & 1 deletion ocboot.sh
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ mkdir -p "$HOME/.ssh"
CMD=""

is_ocboot_subcmd() {
local subcmds="install upgrade add-node add-lbagent backup restore setup-container-env"
local subcmds="install upgrade add-node add-lbagent backup restore setup-container-env switch-edition"
for subcmd in $subcmds; do
if [[ "$1" == "$subcmd" ]]; then
return 0
Expand Down
4 changes: 3 additions & 1 deletion onecloud/ce-services.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
- hosts: all
roles:
- { role: utils/ce-services }
- role: switch-edition
vars:
TARGET_EDITION: ce
4 changes: 3 additions & 1 deletion onecloud/ee-services.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
- hosts: all
roles:
- { role: utils/ee-services }
- role: switch-edition
vars:
TARGET_EDITION: ee
8 changes: 8 additions & 0 deletions onecloud/roles/switch-edition/tasks/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
- name: detect os
include_role:
name: utils/detect-os

- name: "switch to edition {{ TARGET_EDITION }}"
include_role:
name: utils/switch-edition
61 changes: 34 additions & 27 deletions onecloud/roles/utils/ce-services/tasks/main.yml
Original file line number Diff line number Diff line change
@@ -1,29 +1,36 @@
---
- name: switch to CE
environment:
KUBECONFIG: "{{ ENV_KUBECONFIG }}"
PATH: /opt/yunion/bin:{{ ansible_env.PATH }}
shell: |
source <(/opt/yunion/bin/ocadm cluster rcadmin)
ocadm cluster update --use-ce --wait
{{ K3S_CMDLINE_PREFIX }} kubectl -n onecloud patch onecloudcluster default --type='json' -p="[{'op': 'replace', 'path': '/spec/web/imageName', 'value': 'web'}]"
{{ K3S_CMDLINE_PREFIX }} kubectl delete configmap -n onecloud default-web
{{ K3S_CMDLINE_PREFIX }} kubectl rollout restart deployment -n onecloud default-web
sleep 3
while true; do
pod=$(kubectl -n onecloud get pods |grep -v '^NAME' |grep -vi run |awk '{print $1}')
if [ -z "$pod" ]; then
echo all running
break
else
echo "still updating"
fi
sleep 3
done
climc-ee infos-update \
--name " " \
--name-en " " \
--copyright "OneCloud" \
--copyright-en "OneCloud"
args:
executable: /bin/bash
include_role:
name: utils/switch-edition
vars:
TARGET_EDITION: ce

# - name: switch to CE
# environment:
# KUBECONFIG: "{{ ENV_KUBECONFIG }}"
# PATH: /opt/yunion/bin:{{ ansible_env.PATH }}
# shell: |
# # source <(/opt/yunion/bin/ocadm cluster rcadmin)
# # ocadm cluster update --use-ce --wait
# {{ K3S_CMDLINE_PREFIX }} kubectl -n onecloud annotate onecloudcluster default onecloud.yunion.io/edition='ce'
# {{ K3S_CMDLINE_PREFIX }} kubectl -n onecloud patch onecloudcluster default --type='json' -p="[{'op': 'replace', 'path': '/spec/web/imageName', 'value': 'web'}]"
# {{ K3S_CMDLINE_PREFIX }} kubectl delete configmap -n onecloud default-web
# {{ K3S_CMDLINE_PREFIX }} kubectl rollout restart deployment -n onecloud default-web
# sleep 3
# while true; do
# pod=$(kubectl -n onecloud get pods |grep -v '^NAME' |grep -vi run |awk '{print $1}')
# if [ -z "$pod" ]; then
# echo all running
# break
# else
# echo "still updating"
# fi
# sleep 3
# done
# # climc-ee infos-update \
# # --name " " \
# # --name-en " " \
# # --copyright "OneCloud" \
# # --copyright-en "OneCloud"
# args:
# executable: /bin/bash
10 changes: 10 additions & 0 deletions onecloud/roles/utils/switch-edition/tasks/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
- name: Copy switch edition script to /opt/yunion/bin/onecloud-switch-edition.sh
template:
src: "onecloud-switch-edition.sh.j2"
dest: "/opt/yunion/bin/onecloud-switch-edition.sh"
mode: '0755'

- name: "Execute `/opt/yunion/bin/onecloud-switch-edition.sh`, checking log at /tmp/onecloud-switch-edition.log"
shell: "set -o pipefail && bash /opt/yunion/bin/onecloud-switch-edition.sh 2>&1 | tee /tmp/onecloud-switch-edition.log"
args:
executable: /bin/bash
Loading