provision_salt_master.sh 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. #!/bin/bash
  2. SUDO=""
  3. LOG_FILE=/var/log/cloud-init-provision-salt-master-output.log
  4. exec > >(tee ${LOG_FILE}) 2>&1
  5. echo "Started provision_salt_master.sh"
  6. #Install dependencies. The virtual env was used to reduce the python module conflicts between rpm and pip.
  7. echo "Install dependencies"
  8. yum install GitPython --enablerepo=epel -y
  9. #For Version 3001+ Python 3 is required.
  10. #At this point, PY3 should already be installed.
  11. #Thus gitpython needs to be added to python3. required for RHEL 7.
  12. pip3 install gitpython
  13. yum install python-virtualenv -y
  14. virtualenv ~/awscli
  15. #check if proxy settings are ready
  16. if [ -s "/etc/pip.conf" ]; then
  17. ~/awscli/bin/pip install awscli
  18. else
  19. echo "pip proxy not ready"
  20. fi
  21. chmod +x ~/awscli/bin/aws
  22. #we need to refresh our bash session to pick up the proxy settings.
  23. if [ -s "/etc/profile.d/proxy.sh" ]; then
  24. source /etc/profile.d/proxy.sh
  25. else
  26. echo "System proxy not ready"
  27. fi
  28. ~/awscli/bin/aws secretsmanager get-secret-value --region us-gov-east-1 --secret-id saltmaster/ssh_key --query SecretString --output text > ~root/.ssh/github_read_only
  29. chmod 0600 ~root/.ssh/github_read_only
  30. #GPG Keys
  31. echo "GPG Keys"
  32. mkdir -p /etc/salt/gpgkeys
  33. chmod 0700 /etc/salt/gpgkeys
  34. ~/awscli/bin/aws secretsmanager get-secret-value --region us-gov-east-1 --secret-id saltmaster/gpg/private --query SecretString --output text > /etc/salt/gpgkeys/private
  35. ~/awscli/bin/aws secretsmanager get-secret-value --region us-gov-east-1 --secret-id saltmaster/gpg/ownertrust --query SecretString --output text > /etc/salt/gpgkeys/ownertrust
  36. chmod 0600 /etc/salt/gpgkeys/private
  37. chmod 0600 /etc/salt/gpgkeys/ownertrust
  38. gpg --import --yes --batch -q --homedir /etc/salt/gpgkeys/ /etc/salt/gpgkeys/private
  39. gpg --import-ownertrust --homedir /etc/salt/gpgkeys/ /etc/salt/gpgkeys/ownertrust
  40. #Salt Master Pub/Private
  41. ~/awscli/bin/aws secretsmanager get-secret-value --region us-gov-east-1 --secret-id saltmaster/master.pem --query SecretString --output text > /etc/salt/pki/master/master.pem
  42. ~/awscli/bin/aws secretsmanager get-secret-value --region us-gov-east-1 --secret-id saltmaster/master.pub --query SecretString --output text > /etc/salt/pki/master/master.pub
  43. chmod 0400 /etc/salt/pki/master/master.pem
  44. #clean up. These are not needed after initial bootstrapping.
  45. yum remove python-virtualenv -y
  46. rm -rf ~/awscli
  47. cat > ~/.ssh/config << 'EOF'
  48. Host github.mdr.defpoint.com
  49. IdentityFile ~/.ssh/github_read_only
  50. EOF
  51. chmod 0400 ~/.ssh/config
  52. cat - > ~/.ssh/known_hosts << 'EOF'
  53. github.mdr.defpoint.com ecdsa-sha2-nistp256 AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABBBObDNqJCB+iXyR2hm0GRERmfEl33E7Kiu+UGmSHHC878NQjsvOtLxoRAPIU07bCzKutFNZCi+8bRkQWXtOT2InA=
  54. EOF
  55. cat > /etc/salt/master.d/gpg_pillar.conf << 'EOF'
  56. decrypt_pillar:
  57. - 'secrets' : gpg
  58. EOF
  59. #For both Test and Prod start out pointing to the Master branch. After inital startup, it is expected that a highstate will be run which will change the branch in Test to develop.
  60. cat - > /etc/salt/master.d/gitfs.conf << 'EOF'
  61. fileserver_backend:
  62. - gitfs
  63. - roots
  64. gitfs_saltenv_whitelist:
  65. - base
  66. - master
  67. - develop
  68. # File roots via Git
  69. gitfs_provider: gitpython
  70. gitfs_update_interval: 600
  71. gitfs_base: master
  72. gitfs_remotes:
  73. - git@github.mdr.defpoint.com:mdr-engineering/msoc-infrastructure.git:
  74. - name: gitfs-base-msoc
  75. - base: master
  76. - root: salt/fileroots
  77. # File roots for the CM's
  78. - git@github.mdr.defpoint.com:mdr-engineering/msoc-moose-cm.git:
  79. - name: msoc-moose-cm
  80. - base: master
  81. - mountpoint: salt://customer_repos/msoc-moose-cm
  82. - git@github.mdr.defpoint.com:mdr-engineering/msoc-afs-cm.git:
  83. - name: msoc-afs-cm
  84. - base: master
  85. - mountpoint: salt://customer_repos/msoc-afs-cm
  86. - git@github.mdr.defpoint.com:mdr-engineering/msoc-nga-cm.git:
  87. - name: msoc-nga-cm
  88. - base: master
  89. - mountpoint: salt://customer_repos/msoc-nga-cm
  90. # File roots for the deployment servers
  91. - git@github.mdr.defpoint.com:mdr-engineering/msoc-nga-pop.git:
  92. - name: msoc-nga-pop
  93. - base: master
  94. - mountpoint: salt://deployment_servers/msoc-nga-pop
  95. - git@github.mdr.defpoint.com:mdr-engineering/msoc-afs-pop.git:
  96. - name: msoc-afs-pop
  97. - base: master
  98. - mountpoint: salt://deployment_servers/msoc-afs-pop
  99. # Pillar via Git configs
  100. git_pillar_provider: gitpython
  101. git_pillar_root: salt/pillar
  102. git_pillar_base: master
  103. git_pillar_branch: master
  104. ext_pillar:
  105. - git:
  106. - git@github.mdr.defpoint.com:mdr-engineering/msoc-infrastructure.git:
  107. - name: salt-piller-base
  108. - env: base
  109. EOF
  110. systemctl restart salt-master
  111. systemctl enable salt-master
  112. #This attempts to help out with accepting the minion key.
  113. sleep 60
  114. salt-key -A -y
  115. salt-call state.highstate
  116. echo "Ending provision_salt_master.sh"