2017-11-06 10:16:17 +01:00
|
|
|
// Copyright (c) 2017 VMware, Inc. All Rights Reserved.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2017-07-25 04:48:12 +02:00
|
|
|
#!/usr/bin/python
|
2017-11-06 10:16:17 +01:00
|
|
|
|
|
|
|
"""
|
|
|
|
The script is to import the projects of source file into Admiral and save the results into mapprojectsfile.
|
|
|
|
It's only for VIC 1.2 migration.
|
|
|
|
"""
|
|
|
|
|
2017-07-25 04:48:12 +02:00
|
|
|
import json
|
|
|
|
from optparse import OptionParser
|
|
|
|
import os
|
2017-07-31 11:29:19 +02:00
|
|
|
import urllib2, ssl
|
2017-07-25 04:48:12 +02:00
|
|
|
import sys
|
|
|
|
import logging
|
|
|
|
import logging.config
|
|
|
|
|
|
|
|
logging.basicConfig(filename="import_project.log", level=logging.INFO)
|
|
|
|
logger = logging.getLogger()
|
|
|
|
|
|
|
|
class Parameters(object):
|
|
|
|
def __init__(self):
|
|
|
|
self.admiral_endpoint = ''
|
2017-07-26 10:19:49 +02:00
|
|
|
self.tokenfile = ''
|
2017-07-25 04:48:12 +02:00
|
|
|
self.projectsfile = ''
|
2017-09-07 16:50:38 +02:00
|
|
|
self.mapprojectsfile = ''
|
2017-07-25 04:48:12 +02:00
|
|
|
self.init_from_input()
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def parse_input():
|
2017-07-26 10:19:49 +02:00
|
|
|
usage = "usage: %prog [options] <admiralendpoint> <tokenfile> <projectsfile>"
|
2017-07-25 04:48:12 +02:00
|
|
|
parser = OptionParser(usage)
|
|
|
|
parser.add_option("-a", "--admiralendpoint", dest="admiral_endpoint", help="admiral endpoint")
|
2017-07-26 10:19:49 +02:00
|
|
|
parser.add_option("-t", "--tokenfile", dest="tokenfile", help="the path of token file")
|
2017-07-25 04:48:12 +02:00
|
|
|
parser.add_option("-f", "--projectsfile", dest="projectsfile", help="the path of exported json file")
|
2017-09-07 16:50:38 +02:00
|
|
|
parser.add_option("-m", "--mapprojectsfile", dest="mapprojectsfile", help="the path of output projects file for mapping project id")
|
2017-07-25 04:48:12 +02:00
|
|
|
|
|
|
|
(options, args) = parser.parse_args()
|
2017-09-07 16:50:38 +02:00
|
|
|
return (options.admiral_endpoint, options.tokenfile, options.projectsfile, options.mapprojectsfile)
|
2017-07-25 04:48:12 +02:00
|
|
|
|
|
|
|
def init_from_input(self):
|
2017-09-07 16:50:38 +02:00
|
|
|
(self.admiral_endpoint, self.tokenfile, self.projectsfile, self.mapprojectsfile) = Parameters.parse_input()
|
2017-07-25 04:48:12 +02:00
|
|
|
|
|
|
|
class Project:
|
2017-09-07 16:50:38 +02:00
|
|
|
def __init__(self, project_id, name, public):
|
|
|
|
self.project_id = project_id
|
2017-07-25 04:48:12 +02:00
|
|
|
self.project_name = name
|
|
|
|
self.public = public
|
2017-09-07 16:50:38 +02:00
|
|
|
self.index_id = ''
|
2017-07-25 04:48:12 +02:00
|
|
|
|
|
|
|
class Admiral:
|
|
|
|
def __init__(self, admiral_url, token):
|
|
|
|
self.admiral_url = admiral_url + '/projects'
|
|
|
|
self.token = token
|
|
|
|
|
|
|
|
def __import_project(self, project, retry=True):
|
|
|
|
project_data = json.dumps({ "name": project.project_name, "isPublic": project.public,
|
|
|
|
"customProperties": {"__enableContentTrust": False, "__preventVulnerableImagesFromRunning":False,
|
|
|
|
"__preventVulnerableImagesFromRunningSeverity":"high", "__automaticallyScanImagesOnPush":False }})
|
|
|
|
data_len = len(project_data)
|
|
|
|
request = urllib2.Request(self.admiral_url, project_data)
|
|
|
|
request.add_header('x-xenon-auth-token', self.token)
|
|
|
|
request.add_header('Content-Type', 'application/json')
|
|
|
|
request.add_header('Content-Length', data_len)
|
|
|
|
|
|
|
|
try:
|
2017-09-07 16:50:38 +02:00
|
|
|
response = urllib2.urlopen(request, context=ssl._create_unverified_context())
|
|
|
|
response_obj = response.read()
|
|
|
|
project.index_id = json.loads(response_obj)['customProperties']['__projectIndex']
|
2017-07-25 04:48:12 +02:00
|
|
|
except Exception, e:
|
|
|
|
if not retry:
|
|
|
|
logger.error("failed to import project: %s, admiral_endpoint: %s, error: %s " % (project.project_name, self.admiral_url, str(e)))
|
|
|
|
return
|
|
|
|
self.__import_project(project, False)
|
|
|
|
|
|
|
|
def import_project(self, projects):
|
|
|
|
for project in projects:
|
|
|
|
self.__import_project(project)
|
|
|
|
|
|
|
|
def main():
|
|
|
|
commandline_input = Parameters()
|
2017-07-26 10:19:49 +02:00
|
|
|
|
2017-07-25 04:48:12 +02:00
|
|
|
try:
|
|
|
|
if not os.path.exists(commandline_input.projectsfile):
|
|
|
|
raise Exception('Error: %s does not exist' % commandline_input.projectsfile)
|
|
|
|
|
2017-07-26 10:19:49 +02:00
|
|
|
if not os.path.exists(commandline_input.tokenfile):
|
|
|
|
raise Exception('Error: %s does not exist' % commandline_input.tokenfile)
|
|
|
|
|
|
|
|
with open(commandline_input.tokenfile, 'r') as f:
|
|
|
|
token = f.readlines()
|
|
|
|
|
|
|
|
if len(token) == 0:
|
|
|
|
raise Exception('No token found in the properties file %s' % commandline_input.tokenfile)
|
|
|
|
|
|
|
|
admiral = Admiral(commandline_input.admiral_endpoint, token[0])
|
|
|
|
|
2017-07-25 04:48:12 +02:00
|
|
|
with open(commandline_input.projectsfile, 'r') as project_data_file:
|
|
|
|
project_data = json.load(project_data_file)
|
|
|
|
|
|
|
|
projects_import_list = []
|
|
|
|
for item in project_data['projects']:
|
2017-09-07 16:50:38 +02:00
|
|
|
projects_import_list.append(Project(item['project_id'], item['project_name'], item['public']))
|
2017-07-25 04:48:12 +02:00
|
|
|
|
|
|
|
admiral.import_project(projects_import_list)
|
2017-09-07 16:50:38 +02:00
|
|
|
|
|
|
|
with open(commandline_input.mapprojectsfile, 'w') as outfile:
|
|
|
|
json.dump({'map_projects': [project.__dict__ for project in projects_import_list]}, outfile, sort_keys=True, indent=4)
|
2017-07-25 04:48:12 +02:00
|
|
|
|
|
|
|
except Exception, e:
|
|
|
|
logger.error("failed to import project, admiral_endpoint: %s, error: %s " % (commandline_input.admiral_endpoint, str(e)))
|
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
2017-09-07 16:50:38 +02:00
|
|
|
main()
|