github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/acceptancetests/repository/charms/jenkins-juju-ci/hooks/config-changed (about)

     1  #!/bin/bash
     2  set -eux
     3  
     4  HOSTNAME=$(hostname)
     5  JENKINS_HOME="/var/lib/jenkins"
     6  KEY="staging-juju-rsa"
     7  
     8  export PATH=/usr/local/bin:$JENKINS_HOME/Bin:$PATH
     9  
    10  
    11  as_jenkins() {
    12      local_command="$@"
    13      su -l jenkins -c "$local_command"
    14  }
    15  
    16  
    17  update_branch() {
    18      local_branch=$1
    19      local_dir="$(basename $local_branch | cut -d ':' -f2)"
    20      local_path="$JENKINS_HOME/$local_dir"
    21      if [[ -d $local_path ]]; then
    22          as_jenkins bzr pull -d $local_path
    23      else
    24          as_jenkins bzr branch $local_branch $local_path
    25      fi
    26  }
    27  
    28  
    29  update_known_hosts() {
    30      local_host="$1"
    31      local_key="$(ssh-keyscan -H $local_host)"
    32      if ! grep "$local_key" $JENKINS_HOME/.ssh/known_hosts; then
    33          echo "$local_key" >> $JENKINS_HOME/.ssh/known_hosts
    34      fi
    35  }
    36  
    37  
    38  status-set maintenance "Updating scripts" || true
    39  
    40  set +x
    41  LP_KEY=$(config-get lp-key)
    42  if [[ "$LP_KEY" == "" ]]; then
    43      status-set blocked "Waiting for lp-key to be set" || true
    44      exit 0
    45  fi
    46  set -x
    47  
    48  status-set maintenance "Updating bzr" || true
    49  set +x
    50  echo "$LP_KEY" > $JENKINS_HOME/.ssh/lp_rsa
    51  set -x
    52  cp $JENKINS_HOME/.ssh/lp_rsa $JENKINS_HOME/.ssh/id_rsa
    53  chown -R jenkins:jenkins $JENKINS_HOME/.ssh
    54  chmod 600 $JENKINS_HOME/.ssh/*rsa
    55  update_known_hosts bazaar.launchpad.net
    56  update_known_hosts git.launchpad.net
    57  update_known_hosts upload.launchpad.net
    58  update_known_hosts ppa.launchpad.net
    59  chmod 600 $JENKINS_HOME/.ssh/known_hosts
    60  as_jenkins bzr --no-aliases launchpad-login juju-qa-bot
    61  
    62  status-set maintenance "Updating branches" || true
    63  if [[ -d $JENKINS_HOME/ci-director ]]; then
    64      update_branch lp:ci-director
    65  fi
    66  update_branch lp:workspace-runner
    67  update_branch lp:juju-release-tools
    68  update_branch lp:juju-ci-tools
    69  update_branch lp:juju-ci-tools/repository
    70  update_branch lp:~juju-qa/+junk/cloud-city
    71  
    72  status-set maintenance "Updating permissions" || true
    73  chown -R jenkins $JENKINS_HOME/cloud-city
    74  chmod -R go-w $JENKINS_HOME/cloud-city
    75  chmod 700 $JENKINS_HOME/cloud-city
    76  chmod 700 $JENKINS_HOME/cloud-city/gnupg
    77  chmod 600 $JENKINS_HOME/cloud-city/$KEY
    78  
    79  status-set maintenance "Updating ssh" || true
    80  if [[ ! -f $JENKINS_HOME/.ssh/config ]]; then
    81  cat << EOC > $JENKINS_HOME/.ssh/config
    82  Host 10.* 192.168.*
    83    StrictHostKeyChecking no
    84    UserKnownHostsFile /dev/null
    85    User ubuntu
    86    IdentityFile $JENKINS_HOME/cloud-city/$KEY
    87  EOC
    88  fi
    89  if [[ ! -f $JENKINS_HOME/.ssh/id_rsa ]]; then
    90      as_jenkins ln -s $JENKINS_HOME/cloud-city/$KEY $JENKINS_HOME/.ssh/id_rsa
    91  fi
    92  if [[ ! -f $JENKINS_HOME/.ssh/id_rsa.pub ]]; then
    93      as_jenkins ln -s $JENKINS_HOME/cloud-city/$KEY.pub $JENKINS_HOME/.ssh/id_rsa.pub
    94  fi
    95  chown -R jenkins:jenkins $JENKINS_HOME/.ssh
    96  as_jenkins ssh-import-id lp:juju-qa-bot || true
    97  
    98  status-set maintenance "Updating git" || true
    99  github_username=$(sed -r '/github_user/!d; s/.*"(.*)"/\1/; s/%20/ /' $JENKINS_HOME/cloud-city/juju-bot.txt)
   100  github_password=$(sed -r '/github_password/!d; s/.*"(.*)"/\1/; s/%20/ /' $JENKINS_HOME/cloud-city/juju-bot.txt)
   101  cat << EOC > $JENKINS_HOME/.gitconfig
   102  [user]
   103          name = jenkins
   104          email = jenkins@jenkins
   105  [credential]
   106          helper = cache
   107  [credential "https://github.com"]
   108          username = $github_username
   109          password = $github_password
   110  [url "https://"]
   111          insteadOf = http://
   112  EOC
   113  sudo chown jenkins:jenkins $JENKINS_HOME/.gitconfig
   114  
   115  S3_PROXY=$(config-get s3-proxy)
   116  if [[ $S3_PROXY == ""  ]]; then
   117      # Remove s3 proxy changes from .boto and juju-qa.s3cfg.
   118      rm $JENKINS_HOME/.boto || true
   119      (cd cloud-city && as_jenkins bzr revert bzr revert juju-qa.s3cfg || true)
   120  else
   121      S3_PROXY_HOST=$(echo "$S3_PROXY" | cut -d ':' -f 1)
   122      S3_PROXY_PORT=$(echo "$S3_PROXY" | cut -d ':' -f 2)
   123      # Place a .boto file for python scripts
   124      cat << EOC > $JENKINS_HOME/.boto
   125  [Boto]
   126  proxy = $S3_PROXY_HOST
   127  proxy_port = $S3_PROXY_PORT
   128  EOC
   129      chown jenkins:jenkins $JENKINS_HOME/.boto
   130      # Modify cloud-city/juju-qa.s3cfg to use a proxy.
   131      sed -i -r "s/(proxy_host =).*/\1 $S3_PROXY_HOST/; s/(proxy_port =).*/\1 $S3_PROXY_PORT/;" \
   132          $JENKINS_HOME/cloud-city/juju-qa.s3cfg
   133  fi
   134  
   135  status-set maintenance "Updating dependencies from branches" || true
   136  if [[ $(uname) == "Linux" ]]; then
   137      as_jenkins make -C $JENKINS_HOME/juju-ci-tools install-deps
   138      as_jenkins make -C $JENKINS_HOME/workspace-runner install
   139  fi
   140  
   141  NOW=$(date +%Y-%m-%dT%H:%M)
   142  status-set active "$HOSTNAME updated $NOW" || true