Coverage for heliumcli/actions/deploybuild.py: 86.44%

59 statements  

« prev     ^ index     » next       coverage.py v7.3.2, created at 2023-11-22 16:35 +0000

1import os 

2import subprocess 

3 

4import git 

5 

6from .. import utils 

7 

8__author__ = "Alex Laird" 

9__copyright__ = "Copyright 2018, Helium Edu" 

10__version__ = "1.5.0" 

11 

12 

13class DeployBuildAction: 

14 def __init__(self): 

15 self.name = "deploy-build" 

16 self.help = "Deploy the specified build, which may be a versioned release or a branch" 

17 

18 def setup(self, subparsers): 

19 parser = subparsers.add_parser(self.name, help=self.help) 

20 parser.add_argument("version", help="The build version to be deployed, which may be a version or a branch") 

21 parser.add_argument("env", help="The environment to deploy to") 

22 parser.add_argument("--roles", action="store", type=str, nargs="*", 

23 help="Limit the project roles to be deployed") 

24 parser.add_argument("--migrate", action="store_true", help="Install code dependencies and run migrations") 

25 parser.add_argument("--code", action="store_true", help="Only deploy code") 

26 parser.add_argument("--envvars", action="store_true", help="Only deploy environment variables") 

27 parser.add_argument("--conf", action="store_true", 

28 help="Only deploy configuration files and restart necessary services") 

29 parser.add_argument("--ssl", action="store_true", 

30 help="Only deploy SSL certificates and restart necessary services") 

31 parser.set_defaults(action=self) 

32 

33 def run(self, args): 

34 config = utils.get_config() 

35 ansible_dir = utils.get_ansible_dir() 

36 

37 version = args.version.lstrip("v") 

38 

39 if config["projectsRelativeDir"] != ".": 

40 root_dir = os.path.abspath(os.path.join(ansible_dir, "..")) 

41 if os.path.exists(os.path.join(root_dir, ".git")): 

42 repo = git.Repo(root_dir) 

43 try: 

44 repo.git.fetch(tags=True, prune=True, force=True) 

45 except git.GitCommandError as ex: 

46 if ex.status == 128: 

47 print("WARN: if you want to get the latest code updates, verify your network connection.") 

48 else: 

49 raise ex 

50 

51 repo.git.checkout(version) 

52 

53 hosts = utils.parse_hosts_file(args.env) 

54 for host in hosts: 

55 subprocess.call(["ssh", "-t", "{}@{}".format(host[0], host[1]), 

56 config["hostProvisionCommand"]]) 

57 

58 playbook_options = ["--inventory-file={}/hosts/{}".format(ansible_dir, args.env), "-v", 

59 "--extra-vars", "build_version={}".format(version)] 

60 

61 if args.migrate or args.code or args.envvars or args.conf or args.ssl: 

62 tags = [] 

63 if args.code: 

64 tags.append("code") 

65 if args.migrate: 

66 tags.append("migrate") 

67 if args.envvars: 

68 tags.append("envvars") 

69 if args.conf: 

70 tags.append("conf") 

71 if args.ssl: 

72 tags.append("ssl") 

73 playbook_options.append("--tags") 

74 playbook_options.append(",".join(tags)) 

75 

76 if args.roles: 

77 playbook_options.append("--limit") 

78 playbook_options.append(",".join(args.roles)) 

79 

80 subprocess.call(["ansible-playbook"] + playbook_options + ["{}/{}.yml".format(ansible_dir, args.env)])