github.com/shashidharatd/test-infra@v0.0.0-20171006011030-71304e1ca560/jenkins/attach_agent.py (about)

     1  #!/usr/bin/env python
     2  
     3  # Copyright 2016 The Kubernetes Authors.
     4  #
     5  # Licensed under the Apache License, Version 2.0 (the "License");
     6  # you may not use this file except in compliance with the License.
     7  # You may obtain a copy of the License at
     8  #
     9  #     http://www.apache.org/licenses/LICENSE-2.0
    10  #
    11  # Unless required by applicable law or agreed to in writing, software
    12  # distributed under the License is distributed on an "AS IS" BASIS,
    13  # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14  # See the License for the specific language governing permissions and
    15  # limitations under the License.
    16  
    17  """Attach Jenkins agents."""
    18  
    19  import ConfigParser
    20  import sys
    21  
    22  # Todo(krzyzacy): fix the import error
    23  # sudo pip install jenkinsapi
    24  from jenkinsapi import jenkins # pylint: disable=import-error
    25  
    26  EXCLUSIVE = True
    27  SHARED = False
    28  
    29  # TODO: Add 'scalability' label to heavy to not abuse 'build' label.
    30  # TODO(fejta): add light/heavy/pr to tags and replace nodes
    31  INFO = {
    32      'heavy': ('build unittest', 1, EXCLUSIVE),
    33      'light': ('node e2e', 10, EXCLUSIVE),
    34      'pr': ('pull', 1, SHARED),
    35  }
    36  
    37  
    38  def info(host, kind):
    39      """Get host info."""
    40      labels, executors, exclusive = INFO[kind]
    41      return {
    42          'credential_description': 'Jenkins GCE ssh key',
    43          'exclusive': exclusive,
    44          'host': host,
    45          'java_path': '',
    46          'jvm_options': '',
    47          'labels': labels,
    48          'max_num_retries': 0,
    49          'node_description': '',
    50          'num_executors': executors,
    51          'port': 22,
    52          'prefix_start_slave_cmd': '',
    53          'remote_fs': '/var/lib/jenkins',
    54          'retry_wait_time': 0,
    55          'suffix_start_slave_cmd': '',
    56      }
    57  
    58  
    59  def create(api, host, config):
    60      """Create agent."""
    61      delete(api, host)
    62      print 'Creating %s...' % host,
    63      print api.nodes.create_node(host, config)
    64  
    65  
    66  def delete(api, host):
    67      """Delete agent."""
    68      if host in api.nodes:
    69          print 'Deleting %s...' % host,
    70          print api.delete_node(host)
    71  
    72  
    73  def creds(path, section):
    74      """An ini file with a section per master.
    75  
    76      Should look something like this:
    77        [master-a]
    78        user=foo
    79        key=7deadbeef1234098
    80  
    81        [master-b]
    82        user=bar
    83        key=7deadbeef9999999
    84      """
    85      config = ConfigParser.SafeConfigParser()
    86      config.read(path)
    87      return config.get(section, 'user'), config.get(section, 'key')
    88  
    89  
    90  if __name__ == '__main__':
    91      CMD, HOST, KIND, INI, AGENT = sys.argv[1:]  # pylint: disable=unbalanced-tuple-unpacking
    92      USER, KEY = creds(INI, AGENT)
    93      J = jenkins.Jenkins('http://localhost:8080', USER, KEY)
    94  
    95      if sys.argv[1] == 'delete':
    96          delete(J, HOST)
    97      else:
    98          create(J, HOST, info(HOST, KIND))