aboutsummaryrefslogtreecommitdiffhomepage
path: root/infra/gcb/sync.py
blob: 9004678d9ac6f91a0de5fed746925606a0c942e1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#!/usr/bin/env python
"""Script to sync CF and Jenkins jobs."""

import json
import os
import re
import sys
import yaml

import jenkins

JENKINS_SERVER = ('localhost', 8080)

JOB_TEMPLATES = [
    {'prefix': 'projects/', 'config': 'base_job.xml'},
    {'prefix': 'coverage/', 'config': 'coverage_job.xml'},
]

SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__))
OSSFUZZ_DIR = os.path.dirname(os.path.dirname(SCRIPT_DIR))

VALID_PROJECT_NAME = re.compile(r'^[a-zA-Z0-9_-]+$')


def main():
  # Connect to jenkins server.
  jenkins_login = get_jenkins_login()
  server = jenkins.Jenkins(
      'http://%s:%d' % JENKINS_SERVER,
      username=jenkins_login[0],
      password=jenkins_login[1])

  for project in get_projects():
    print 'syncing configs for', project
    try:
      # Create/update jenkins build job.
      sync_jenkins_job(server, project)

    except Exception as e:
      print >> sys.stderr, 'Failed to setup job with exception', e


def _has_dockerfile(project_dir):
  """Whether or not the project has a Dockerfile."""
  if os.path.exists(os.path.join(project_dir, 'Dockerfile')):
    return True

  project_yaml_path = os.path.join(project_dir, 'project.yaml')
  if not os.path.exists(project_yaml_path):
    return False

  with open(project_yaml_path) as f:
    project_info = yaml.safe_load(f)

  return 'dockerfile' in project_info


def get_projects():
  """Return list of projects for oss-fuzz."""
  projects = []
  projects_dir = os.path.join(OSSFUZZ_DIR, 'projects')
  for name in os.listdir(projects_dir):
    full_path = os.path.join(projects_dir, name)
    if not os.path.isdir(full_path) or not _has_dockerfile(full_path):
      continue

    if not VALID_PROJECT_NAME.match(name):
      print >> sys.stderr, 'Invalid project name:', name
      continue

    projects.append(name)

  if not projects:
    print >> sys.stderr, 'No projects found.'

  return projects


def get_jenkins_login():
  """Returns (username, password) for jenkins."""
  username = os.getenv('JENKINS_USER')
  password = os.getenv('JENKINS_PASS')

  return username, password


def sync_jenkins_job(server, project):
  """Sync the config with jenkins."""
  project_yaml = os.path.join(OSSFUZZ_DIR, 'projects', project, 'project.yaml')
  with open(project_yaml, 'r') as f:
    project_json_string = json.dumps(json.dumps(yaml.safe_load(f)))

  for job in JOB_TEMPLATES:
    job_name = job['prefix'] + project
    with open(os.path.join(SCRIPT_DIR, 'jenkins_config', job['config'])) as f:
      job_config_xml = f.read()

    if server.job_exists(job_name):
      server.reconfig_job(job_name, job_config_xml)
    else:
      server.create_job(job_name, job_config_xml)
      server.build_job(job_name)


if __name__ == '__main__':
  main()