forked from nexcess/python-r1soft
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cdp-add-agent.py
148 lines (139 loc) · 6.06 KB
/
cdp-add-agent.py
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#!/usr/bin/env python
import suds.client
import logging
logger = logging.getLogger('cdp-add-agent')
logger.addHandler(logging.StreamHandler())
logger.setLevel(logging.DEBUG)
logger.propagate = False
class MetaClient(object):
def __init__(self, url_base, **kwargs):
self.__url_base = url_base
self.__init_args = kwargs
self.__clients = dict()
def __getattr__(self, name):
c = self.__clients.get(name, None)
logger.debug('Accessing SOAP client: %s' % name)
if c is None:
logger.debug('Client doesn\'t exist, creating: %s' % name)
c = suds.client.Client(self.__url_base % name, **self.__init_args)
self.__clients[name] = c
return c
def get_wsdl_url(hostname, namespace, use_ssl=True, port_override=None):
if use_ssl:
proto = 'https'
else:
proto = 'http'
if port_override is None:
if use_ssl:
port = 9443
else:
port = 9080
else:
port = port_override
url = '%s://%s:%d/%s?wsdl' % (proto, hostname, port, namespace)
logging.debug('Creating WSDL URL: %s' % url)
return url
if __name__ == '__main__':
import sys
import optparse
import os
parser = optparse.OptionParser()
parser.add_option('-r', '--r1soft-host', dest='cdp_host',
help='R1Soft server to add host to')
parser.add_option('-u', '--username', dest='username',
default=os.environ.get('CDP_USER', 'admin'),
help='R1Soft server API username')
parser.add_option('-p', '--password', dest='password',
default=os.environ.get('CDP_PASS', ''),
help='R1Soft server API password')
parser.add_option('-d', '--description', dest='description',
default=None,
help='Custom description field, default is the same as hostname, applied to all hosts')
parser.add_option('-D', '--use-db-addon', dest='use_db_addon',
action='store_true', default=False,
help='Use the CDP DB addon')
parser.add_option('--db-user', dest='sqluser',
help='MySQL user for DB addon')
parser.add_option('--db-pass', dest='sqlpass',
help='MySQL pass for DB addon')
parser.add_option('-R', '--recovery-point-limit', dest='recovery_point_limit',
type=int, default=30,
help='Number of recovery points to keep')
options, args = parser.parse_args()
cdp_host = options.cdp_host
username = options.username
password = options.password
use_db_addon = options.use_db_addon
recovery_point_limit = options.recovery_point_limit
sqluser = options.sqluser
sqlpass = options.sqlpass
for hostname in args:
if options.description is None:
description = hostname
else:
description = '%s (%s)' % (options.description, hostname)
logger.info('Setting up backups for host (%s) on CDP server (%s) with description: %s',
hostname, cdp_host, description)
client = MetaClient(get_wsdl_url(cdp_host, '%s'),
username=username, password=password)
logger.debug('Creating special types...')
CompressionType = client.DiskSafe.factory.create('diskSafe.compressionType')
CompressionLevel = client.DiskSafe.factory.create('diskSafe.compressionLevel')
DeviceBackupType = client.DiskSafe.factory.create('diskSafe.deviceBackupType')
FrequencyType = client.Policy2.factory.create('frequencyType')
FrequencyValues = client.Policy2.factory.create('frequencyValues')
logger.debug('Created special types')
logger.debug('Getting volumes...')
volumes = client.Volume.service.getVolumes()
volume = volumes[0]
logger.info('Found %d volumes, using volume %s', len(volumes), volume.name)
logger.debug('Creating agent for host: %s', hostname)
agent = client.Agent.service.createAgent(
hostname=hostname,
portNumber=1167,
description=description,
databaseAddOnEnabled=use_db_addon
)
logger.info('Created agent for host (%s) with ID: %s', hostname, agent.id)
logger.debug('Creating disksafe for agent (%s) on volume (%s)', agent.id, volume.id)
disksafe = client.DiskSafe.service.createDiskSafeOnVolume(
name=hostname,
agentID=agent.id,
volumeID=volume.id,
compressionType=CompressionType.QUICKLZ,
compressionLevel=CompressionLevel.LOW,
deviceBackupType=DeviceBackupType.AUTO_ADD_DEVICES,
protectStorageConfiguration=True,
protectUnmountedDevices=False
)
logger.info('Created disksafe with ID: %s', disksafe.id)
FrequencyValues.hoursOfDay = [0]
FrequencyValues.startingMinute = 0
logger.debug('Creating policy for agent (%s) on disksafe (%s)',
hostname, disksafe.id)
policy = client.Policy2.factory.create('policy')
policy.enabled = True
policy.name = hostname
policy.description = description
policy.diskSafeID = disksafe.id
policy.mergeSchedulFrequencyType = FrequencyType.ON_DEMAND
policy.replicationScheduleFrequencyType = FrequencyType.DAILY
policy.replicationScheduleFrequencyValues = FrequencyValues
policy.recoveryPointLimit = recovery_point_limit
policy.forceFullBlockScan = False
if use_db_addon:
dbi = client.Policy2.factory.create('databaseInstance')
dbi.dataBaseType = client.Policy2.factory.create('dataBaseType').MYSQL
dbi.enabled = True
dbi.hostName = '127.0.0.1'
dbi.name = 'default'
dbi.username = sqluser
dbi.password = sqlpass
dbi.portNumber = 3306
dbi.useAlternateDataDirectory = False
dbi.useAlternateHostname = True
dbi.useAlternateInstallDirectory = False
policy.databaseInstanceList = [dbi]
policy = client.Policy2.service.createPolicy(policy=policy)
logger.info('Created policy with ID: %s', policy.id)
logger.info('Finished setting up backups for host: %s', hostname)