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

59 statements  

« prev     ^ index     » next       coverage.py v7.3.2, created at 2023-11-22 17:03 +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, 

45 force=os.environ.get("HELIUMCLI_FORCE_FETCH", "False") == "True") 

46 except git.GitCommandError as ex: 

47 if ex.status == 128: 

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

49 else: 

50 raise ex 

51 

52 repo.git.checkout(version) 

53 

54 hosts = utils.parse_hosts_file(args.env) 

55 for host in hosts: 

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

57 config["hostProvisionCommand"]]) 

58 

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

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

61 

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

63 tags = [] 

64 if args.code: 

65 tags.append("code") 

66 if args.migrate: 

67 tags.append("migrate") 

68 if args.envvars: 

69 tags.append("envvars") 

70 if args.conf: 

71 tags.append("conf") 

72 if args.ssl: 

73 tags.append("ssl") 

74 playbook_options.append("--tags") 

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

76 

77 if args.roles: 

78 playbook_options.append("--limit") 

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

80 

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