provision_salt_master.sh 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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. yum install python-virtualenv -y
  10. virtualenv ~/awscli
  11. #check if proxy settings are ready
  12. if [ -s "/etc/pip.conf" ]; then
  13. ~/awscli/bin/pip install awscli
  14. else
  15. echo "pip proxy not ready"
  16. fi
  17. chmod +x ~/awscli/bin/aws
  18. #we need to refresh our bash session to pick up the proxy settings.
  19. if [ -s "/etc/profile.d/proxy.sh" ]; then
  20. source /etc/profile.d/proxy.sh
  21. else
  22. echo "System proxy not ready"
  23. fi
  24. ~/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
  25. chmod 0600 ~root/.ssh/github_read_only
  26. #GPG Keys
  27. echo "GPG Keys"
  28. mkdir -p /etc/salt/gpgkeys
  29. chmod 0700 /etc/salt/gpgkeys
  30. ~/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
  31. ~/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
  32. chmod 0600 /etc/salt/gpgkeys/private
  33. chmod 0600 /etc/salt/gpgkeys/ownertrust
  34. gpg --import --yes --batch -q --homedir /etc/salt/gpgkeys/ /etc/salt/gpgkeys/private
  35. gpg --import-ownertrust --homedir /etc/salt/gpgkeys/ /etc/salt/gpgkeys/ownertrust
  36. #Salt Master Pub/Private
  37. ~/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
  38. ~/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
  39. chmod 0400 /etc/salt/pki/master/master.pem
  40. #clean up. These are not needed after initial bootstrapping.
  41. yum remove python-virtualenv -y
  42. rm -rf ~/awscli
  43. cat > ~/.ssh/config << 'EOF'
  44. Host github.mdr.defpoint.com
  45. IdentityFile ~/.ssh/github_read_only
  46. EOF
  47. chmod 0400 ~/.ssh/config
  48. cat - > ~/.ssh/known_hosts << 'EOF'
  49. github.mdr.defpoint.com ecdsa-sha2-nistp256 AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABBBObDNqJCB+iXyR2hm0GRERmfEl33E7Kiu+UGmSHHC878NQjsvOtLxoRAPIU07bCzKutFNZCi+8bRkQWXtOT2InA=
  50. EOF
  51. cat > /etc/salt/master.d/gpg_pillar.conf << 'EOF'
  52. decrypt_pillar:
  53. - 'secrets' : gpg
  54. EOF
  55. #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.
  56. cat - > /etc/salt/master.d/gitfs.conf << 'EOF'
  57. fileserver_backend:
  58. - gitfs
  59. - roots
  60. gitfs_saltenv_whitelist:
  61. - base
  62. - master
  63. - develop
  64. # File roots via Git
  65. gitfs_provider: gitpython
  66. gitfs_update_interval: 600
  67. gitfs_base: master
  68. gitfs_remotes:
  69. - git@github.mdr.defpoint.com:mdr-engineering/msoc-infrastructure.git:
  70. - name: gitfs-base-msoc
  71. - base: master
  72. - root: salt/fileroots
  73. # File roots for the CM's
  74. - git@github.mdr.defpoint.com:mdr-engineering/msoc-moose-cm.git:
  75. - name: msoc-moose-cm
  76. - base: master
  77. - mountpoint: salt://customer_repos/msoc-moose-cm
  78. - git@github.mdr.defpoint.com:mdr-engineering/msoc-afs-cm.git:
  79. - name: msoc-afs-cm
  80. - base: master
  81. - mountpoint: salt://customer_repos/msoc-afs-cm
  82. - git@github.mdr.defpoint.com:mdr-engineering/msoc-nga-cm.git:
  83. - name: msoc-nga-cm
  84. - base: master
  85. - mountpoint: salt://customer_repos/msoc-nga-cm
  86. # File roots for the deployment servers
  87. - git@github.mdr.defpoint.com:mdr-engineering/msoc-nga-pop.git:
  88. - name: msoc-nga-pop
  89. - base: master
  90. - mountpoint: salt://deployment_servers/msoc-nga-pop
  91. - git@github.mdr.defpoint.com:mdr-engineering/msoc-afs-pop.git:
  92. - name: msoc-afs-pop
  93. - base: master
  94. - mountpoint: salt://deployment_servers/msoc-afs-pop
  95. # Pillar via Git configs
  96. git_pillar_provider: gitpython
  97. git_pillar_root: salt/pillar
  98. git_pillar_base: master
  99. git_pillar_branch: master
  100. ext_pillar:
  101. - git:
  102. - git@github.mdr.defpoint.com:mdr-engineering/msoc-infrastructure.git:
  103. - name: salt-piller-base
  104. - env: base
  105. EOF
  106. systemctl restart salt-master
  107. systemctl enable salt-master
  108. #This attempts to help out with accepting the minion key.
  109. sleep 60
  110. salt-key -A -y
  111. salt-call state.highstate
  112. echo "Ending provision_salt_master.sh"