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

61 statements  

« prev     ^ index     » next       coverage.py v7.3.2, created at 2023-11-21 23:41 +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 if config["projectsRelativeDir"] != ".": 

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

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

40 repo = git.Repo(root_dir) 

41 try: 

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

43 except git.GitCommandError as ex: 

44 if ex.status == 128: 

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

46 else: 

47 raise ex 

48 

49 if len(repo.git.diff(args.version, "master")) > 0: 

50 repo.git.checkout(args.version) 

51 else: 

52 repo.git.checkout("master") 

53 

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

55 hosts = utils.parse_hosts_file(args.env) 

56 for host in hosts: 

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

58 config["hostProvisionCommand"]]) 

59 

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

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

62 

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

64 tags = [] 

65 if args.code: 

66 tags.append("code") 

67 if args.migrate: 

68 tags.append("migrate") 

69 if args.envvars: 

70 tags.append("envvars") 

71 if args.conf: 

72 tags.append("conf") 

73 if args.ssl: 

74 tags.append("ssl") 

75 playbook_options.append("--tags") 

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

77 

78 if args.roles: 

79 playbook_options.append("--limit") 

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

81 

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