-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathship
More file actions
executable file
·1586 lines (1420 loc) · 60.5 KB
/
ship
File metadata and controls
executable file
·1586 lines (1420 loc) · 60.5 KB
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python
# Copyright 2011 Gilt Groupe, INC
#
# 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.
# System modules
import os
import sys
import cmdln
import socket
import types
import datetime
import time
# Extra modules
sys.path.append("/usr/lib/python2.4/site-packages/SQLAlchemy-0.5.5-py2.4.egg/")
import sqlalchemy
from pprint import pprint
import yaml
# Our modules
import mothership
import mothership.kv
import mothership.xen
import mothership.cobbler
import mothership.puppet
import mothership.serverinfo
import mothership.mgmt_vlan
import mothership.dns
import mothership.users
import mothership.list_values
import mothership.snmp
import mothership.zenoss
import mothership.ldap
from mothership.list_servers import *
from mothership.mothership_models import *
from mothership.configure import Configure
import mothership.zabbix
def is_ship_allowed_to_run(config):
if config.min_time and config.max_time:
tzone = False
try:
tzone = os.environ['TZ']
except KeyError:
pass
os.environ['TZ'] = 'US/Eastern'
now = datetime.datetime.now()
if tzone:
os.environ['TZ'] = tzone
else:
os.environ['TZ'] = ''
if now.hour > config.min_time and now.hour < config.max_time:
return False
else:
return True
else:
return True
class ShipCli(cmdln.Cmdln):
def __init__(self, cfg):
cmdln.Cmdln.__init__(self)
self.cfg = cfg
self.name = "ship"
# Zabbix API interaction
@cmdln.alias("zabbix_api")
@cmdln.alias("zab")
@cmdln.alias("zbx")
@cmdln.option("-a", "--add", action="store_true",
help="add a server to zabbix")
@cmdln.option("-r", "--remove", action="store_true",
help="remove a server from zabbix")
@cmdln.option("-e", "--enable", action="store_true",
help="enable monitoring on a server")
@cmdln.option("-d", "--disable", action="store_true",
help="disable monitoring on a server")
@cmdln.option("-s", "--zabbix_server",
help="specify a zabbix server to connect to")
@cmdln.option("-t", "--zabbix_template",
help="specify a zabbix template to assign to this host")
def do_zabbix(self, subcmd, opts, hostname):
"""${cmd_name}: manipulate zabbix info for a host
${cmd_usage}
${cmd_option_list}
"""
# get some basic server info
host,realm,site_id = mothership.get_unqdn(self.cfg, hostname)
unqdn = '.'.join([host,realm,site_id])
# get some Zabbix server info
if opts.zabbix_server:
zs_host,zs_realm,zs_site_id = mothership.get_unqdn(self.cfg, opts.zabbix_server)
zs_unqdn = '.'.join([zs_host,zs_realm,zs_site_id])
else:
zs_unqdn = None
if opts.add:
if opts.zabbix_template:
mothership.zabbix.add(self.cfg, unqdn, zs_unqdn, zabbix_template=opts.zabbix_template)
else:
mothership.zabbix.add(self.cfg, unqdn, zs_unqdn, zabbix_template=None)
elif opts.remove:
mothership.zabbix.remove(self.cfg, unqdn, zs_unqdn)
elif opts.enable:
if opts.disable:
print "Incompatible options -d and -e"
return
else:
mothership.zabbix.enable(self.cfg, unqdn, zs_unqdn)
elif opts.disable:
if opts.enable:
print "Incompatible options -d and -e"
return
else:
mothership.zabbix.disable(self.cfg, unqdn, zs_unqdn)
else:
mothership.zabbix.display(self.cfg, unqdn, zs_unqdn)
# Capistrano functions, this needs expansion
@cmdln.alias("cap")
def do_capistrano(self, subcmd, opts):
"""${cmd_name}: manipulate capistrano data, default writes out a cap config 'tag' section
${cmd_usage}
${cmd_option_list}
"""
mothership.cap_write_config(self.cfg)
# Management vlan interaction
@cmdln.alias("mgmtvlan")
@cmdln.alias("mvl")
@cmdln.option("-e", "--enable", action="store_true",
help="enable a server's management port")
@cmdln.option("-d", "--disable", action="store_true",
help="disable a server's management port")
def do_mgmt_vlan(self, subcmd, opts, hostname):
"""${cmd_name}: manipulate a server's management port
${cmd_usage}
${cmd_option_list}
"""
# get some basic server info
host,realm,site_id = mothership.get_unqdn(self.cfg, hostname)
if opts.enable:
mothership.mgmt_vlan.enable(self.cfg, host, realm, site_id)
elif opts.disable:
mothership.mgmt_vlan.disable(self.cfg, host, realm, site_id)
else:
mothership.mgmt_vlan.status(self.cfg, host, realm, site_id)
# Add users, ldap edition
@cmdln.alias("useradd")
@cmdln.alias("uadd")
@cmdln.alias("addu")
@cmdln.alias("add_user")
@cmdln.alias("user_add")
@cmdln.alias("adduser")
@cmdln.option("-c", "--copy_from",
help="copy type, shell, and groups from a user")
@cmdln.option("-k", "--keyfile",
help="ssh2 public key file to read")
@cmdln.option("-u", "--uid", type="int",
help="assign a particular uid to a user")
@cmdln.option("-H", "--homedir",
help="assign an alternate homedir to a user (default is /home/username)")
@cmdln.option("-s", "--shell",
help="assign an alternate shell to a user (default is /bin/bash)")
@cmdln.option("-e", "--email",
help="assign an alternate email to a user (default is username@email_domain)")
@cmdln.option("-t", "--type",
help="type of user")
@cmdln.option("-l", "--ldap", action="store_true",
help="Add user to LDAP server")
def do_add_users(self, subcmd, opts, username, first_name, last_name):
"""${cmd_name}: add users to mothership. to assign groups, use "ugmap"
${cmd_usage}
${cmd_option_list}
"""
try:
mothership.users.uadd(self.cfg, username, first_name, last_name, copy_from=opts.copy_from, keyfile=opts.keyfile, uid=opts.uid, hdir=opts.homedir, shell=opts.shell, email=opts.email, user_type=opts.type)
except Exception, e:
print "Error: %s" % e
if opts.ldap:
if not self.cfg.ldap_active:
print "LDAP functionality is inactive.\n--ldap (-l) is only useful if you turn LDAP on in the config"
sys.exit(1)
else:
try:
mothership.ldap.add_user(self.cfg, username)
except Exception, e:
print 'Error: %s' % e
# Remove users
@cmdln.alias("rmuser")
@cmdln.alias("rmusers")
@cmdln.alias("rm_user")
@cmdln.alias("deluser")
@cmdln.alias("userdel")
@cmdln.alias("user_del")
@cmdln.alias("del_user")
def do_rm_users(self, subcmd, opts, username):
"""${cmd_name}: completely remove users from mothership
${cmd_usage}
${cmd_option_list}
"""
try:
mothership.users.uremove(self.cfg, username)
except Exception, e:
print 'Error: %s' % e
# Disable/deactivate users
@cmdln.alias("de_user")
@cmdln.alias("disable_user")
@cmdln.alias("disable_users")
@cmdln.alias("deactivate_user")
@cmdln.alias("deactivate_users")
@cmdln.alias("deuser")
@cmdln.alias("disuser")
@cmdln.alias("deusers")
@cmdln.alias("disusers")
def do_de_users(self, subcmd, opts, username):
"""${cmd_name}: disable users stored within mothership
${cmd_usage}
${cmd_option_list}
"""
try:
mothership.users.udeactivate(self.cfg, username)
except Exception, e:
print 'Error: %s' % e
# enable/activate users
@cmdln.alias("en_user")
@cmdln.alias("act_user")
@cmdln.alias("enable_user")
@cmdln.alias("enable_users")
@cmdln.alias("activate_user")
@cmdln.alias("activate_users")
@cmdln.alias("enuser")
@cmdln.alias("actuser")
@cmdln.alias("enusers")
@cmdln.alias("actusers")
def do_en_users(self, subcmd, opts, username):
"""${cmd_name}: enable users stored within mothership.
${cmd_usage}
${cmd_option_list}
"""
try:
mothership.users.uactivate(self.cfg, username)
except Exception, e:
print 'Error: %s' % e
# Modify users
@cmdln.alias("moduser")
@cmdln.alias("mu")
@cmdln.alias("usermod")
@cmdln.alias("modusers")
@cmdln.alias("usersmod")
@cmdln.alias("mod_user")
@cmdln.alias("user_mod")
@cmdln.alias("users_mod")
@cmdln.option("-k", "--keyfile",
help="ssh2 public key file to read")
@cmdln.option("-f", "--fname",
help="user's first name")
@cmdln.option("-l", "--lname",
help="user's last name")
@cmdln.option("-u", "--uid", type="int",
help="assign a particular uid to a user")
@cmdln.option("-H", "--homedir",
help="assign an alternate homedir to a user (default is /home/username)")
@cmdln.option("-s", "--shell",
help="assign an alternate shell to a user (default is /bin/bash)")
@cmdln.option("-e", "--email",
help="assign an alternate email to a user (default is username@email_domain)")
@cmdln.option("-t", "--type",
help="type of user")
def do_mod_users(self, subcmd, opts, username):
"""${cmd_name}: modify users stored within mothership
${cmd_usage}
${cmd_option_list}
"""
try:
mothership.users.umodify(self.cfg, username, first_name=opts.fname, last_name=opts.lname, keyfile=opts.keyfile, uid=opts.uid, hdir=opts.homedir, shell=opts.shell, email=opts.email, user_type=opts.type)
except Exception, e:
print 'Error: %s' % e
# clone users
@cmdln.alias("cloneuser")
@cmdln.alias("userclone")
@cmdln.alias("cloneu")
@cmdln.alias("uclone")
def do_clone_user(self, subcmd, opts, username, newfqn):
"""${cmd_name}: clone a user into a new realm.site_id
${cmd_usage}
${cmd_option_list}
"""
try:
mothership.users.uclone(self.cfg, username, newfqn)
except Exception, e:
print 'Error: %s' % e
# write a user's ssh public key to a file
@cmdln.alias("writekey")
@cmdln.alias("write_key")
@cmdln.alias("write_pubkey")
@cmdln.alias("write_ssh_pubkey")
@cmdln.alias("writesshkey")
@cmdln.alias("wk")
@cmdln.alias("wspk")
@cmdln.option("-k", "--keyfile",
help="ssh2 public key file to write")
def do_write_ssh_public_key(self, subcmd, opts, username):
"""${cmd_name}: write out a user's ssh public key
${cmd_usage}
${cmd_option_list}
"""
try:
mothership.users.uwrite_pubkey(self.cfg, username, opts.keyfile)
except Exception, e:
print 'Error: %s' % e
# User display
@cmdln.alias("user")
@cmdln.alias("udisplay")
@cmdln.alias("displayusers")
@cmdln.alias("displayuser")
@cmdln.alias("user_display")
@cmdln.alias("display_user")
@cmdln.alias("ud")
@cmdln.alias("du")
@cmdln.option("-p", "--pubkey", action="store_true",
help="display public key, only useful with -d")
@cmdln.option("-c", "--compact", action="store_true",
help="display user in compact form, only useful with -d")
@cmdln.option("-l", "--list_groups", action="store_true",
help="display the user's groups in a list, one on each line")
def do_display_users(self, subcmd, opts, username):
"""${cmd_name}: display users stored within mothership
${cmd_usage}
${cmd_option_list}
"""
try:
mothership.users.udisplay(self.cfg, username, pubkey=opts.pubkey, compact=opts.compact, list=opts.list_groups)
except Exception, e:
print 'Error: %s' % e
# Add a group
@cmdln.alias("addgroup")
@cmdln.alias("groupadd")
@cmdln.alias("add_groups")
@cmdln.alias("group_add")
@cmdln.alias("addg")
@cmdln.alias("gadd")
@cmdln.option("-g", "--gid", type="int",
help="assign a particular GID to a group")
@cmdln.option("-d", "--desc",
help="a description for the group")
@cmdln.option("-s", "--sudo_cmds",
help="command restrictions for sudoers generation (ALL is valid here). an empty value will cause the group to be ignored by generate_sudoers")
def do_add_group(self, subcmd, opts, groupname):
"""${cmd_name}: add a group to mothership
${cmd_usage}
${cmd_option_list}
"""
try:
mothership.users.gadd(self.cfg, groupname, gid=opts.gid, description=opts.desc, sudo_cmds=opts.sudo_cmds)
except Exception, e:
print 'Error: %s' % e
# Remove a group
@cmdln.alias("removegroup")
@cmdln.alias("group_remove")
@cmdln.alias("groupremove")
@cmdln.alias("rmgroup")
@cmdln.alias("rmg")
@cmdln.alias("grm")
@cmdln.alias("gremove")
def do_remove_group(self, subcmd, opts, groupname):
"""${cmd_name}: remove a group from mothership
${cmd_usage}
${cmd_option_list}
"""
try:
mothership.users.gremove(self.cfg, groupname)
except Exception, e:
print 'Error: %s' % e
# Modify a group
@cmdln.alias("gmod")
@cmdln.alias("group_modify")
@cmdln.alias("modgroup")
@cmdln.alias("gm")
@cmdln.alias("mg")
@cmdln.alias("modifygroup")
@cmdln.alias("groupmodify")
@cmdln.alias("groupmod")
@cmdln.option("-g", "--gid", type="int",
help="assign a particular GID to a group")
@cmdln.option("-d", "--desc",
help="a description for the group")
@cmdln.option("-s", "--sudo_cmds",
help="command restrictions for sudoers generation (ALL is valid here). an empty value will cause the group to be ignored by generate_sudoers")
def do_modify_group(self, subcmd, opts, groupname):
"""${cmd_name}: modify a group within mothership
${cmd_usage}
${cmd_option_list}
"""
try:
mothership.users.gmodify(self.cfg, groupname, gid=opts.gid, description=opts.desc, sudo_cmds=opts.sudo_cmds)
except Exception, e:
print 'Error: %s' % e
# Display a group
@cmdln.alias("gdisplay")
@cmdln.alias("gd")
@cmdln.alias("gidsp")
@cmdln.alias("display_group")
@cmdln.alias("displaygroup")
@cmdln.option("-l", "--list_users", action="store_true",
help="display the group members in a list, one on each line")
def do_group_display(self, subcmd, opts, groupname):
"""${cmd_name}: display a group within mothership
${cmd_usage}
${cmd_option_list}
"""
try:
mothership.users.gdisplay(self.cfg, groupname, opts.list_users)
except Exception, e:
print 'Error: %s' % e
# User/Group mapping manipulation
@cmdln.alias("ugmap")
@cmdln.option("-a", "--add", action="store_true",
help="add a user to a group")
@cmdln.option("-r", "--remove", action="store_true",
help="remove a user from a group")
@cmdln.option("-f", "--force", action="store_true",
help="Force a user creation, it means no question will be asked.")
def do_user_group_mapping(self, subcmd, opts, username, groupname):
"""${cmd_name}: manipulate user-to-group mappings within mothership
${cmd_usage}
${cmd_option_list}
"""
if opts.force:
force = True
else:
force = False
try:
if opts.add and opts.remove:
print "-a and -r are mutually exclusive, pick one."
sys.exit(1)
elif opts.add:
mothership.users.utog(self.cfg, username, groupname, force)
elif opts.remove:
mothership.users.urmg(self.cfg, username, groupname, force)
else:
# write a display
print "display functionality is not yet working, check back later"
except Exception, e:
print "Error: %s" % e
# LDAP section. only present these commands if we're configured for LDAP in mothership.yaml
# add a user to the ldap db
@cmdln.alias("lduadd")
@cmdln.alias("ldapuadd")
@cmdln.alias("ldapuseradd")
def do_ldap_user_add(self, subcmd, opts, username):
"""${cmd_name}: LDAP user add
${cmd_usage}
${cmd_option_list}
"""
if self.cfg.ldap_active:
try:
mothership.ldap.uadd(self.cfg, username)
except Exception, e:
print "Error: %s" % e
# remove a user from the ldap db
@cmdln.alias("ldurm")
@cmdln.alias("lduserrm")
@cmdln.alias("lduremove")
@cmdln.alias("lduserremove")
@cmdln.alias("ldapurm")
@cmdln.alias("ldapuserremove")
@cmdln.alias("ldap_urm")
def do_ldap_user_remove(self, subcmd, opts, username):
"""${cmd_name}: LDAP user remove
${cmd_usage}
${cmd_option_list}
"""
try:
mothership.ldap.uremove(self.cfg, username)
except Exception, e:
print "Error: %s" % e
# update a user's ldap entry
@cmdln.alias("lduupdate")
@cmdln.alias("lduu")
@cmdln.alias("ldumodify")
@cmdln.alias("ldapuupdate")
@cmdln.alias("ldapuserupdate")
@cmdln.alias("ldapuu")
def do_ldap_user_update(self, subcmd, opts, username):
"""${cmd_name}: LDAP user update
${cmd_usage}
${cmd_option_list}
"""
try:
mothership.ldap.uupdate(self.cfg, username)
except Exception, e:
print "Error: %s" % e
# update a user's ldap password
@cmdln.alias("ldpw")
@cmdln.alias("ldappassword")
@cmdln.alias("ldappass")
@cmdln.alias("ldappwd")
def do_ldap_user_passwd(self, subcmd, opts, username):
"""${cmd_name}: LDAP user password update
${cmd_usage}
${cmd_option_list}
"""
try:
mothership.ldap.update_ldap_passwd(self.cfg, username)
except Exception, e:
print "Error: %s" % e
# refresh all users in the ldap db (dangerous)
@cmdln.alias("ldrefresh")
@cmdln.alias("ld_refresh")
@cmdln.alias("ldaprefresh")
@cmdln.option("-u", "--users", action="store_true",
help="refresh the users in ldap")
@cmdln.option("-g", "--groups", action="store_true",
help="refresh the groups in ldap")
def do_ldap_refresh(self, subcmd, opts, realm_path):
"""${cmd_name}: LDAP refresh, takes realm.site_id as its argument
${cmd_usage}
${cmd_option_list}
"""
try:
if opts.users:
mothership.ldap.urefresh(self.cfg, realm_path)
if opts.groups:
mothership.ldap.grefresh(self.cfg, realm_path)
if not opts.groups and not opts.users:
mothership.ldap.urefresh(self.cfg, realm_path)
mothership.ldap.grefresh(self.cfg, realm_path)
except Exception, e:
print "Error: %s" % e
# display a user's ldap entry
@cmdln.alias("ldud")
@cmdln.alias("ldapudisplay")
@cmdln.alias("ldapuserdisplay")
@cmdln.alias("ldapdisplayuser")
@cmdln.alias("ldap_udisplay")
def do_ldap_user_display(self, subcmd, opts, username):
"""${cmd_name}: LDAP user display
${cmd_usage}
${cmd_option_list}
"""
try:
mothership.ldap.udisplay(self.cfg, username)
except Exception, e:
print "Error: %s" % e
# add a group to ldap's db
@cmdln.alias("ldgadd")
@cmdln.alias("ldapgadd")
@cmdln.alias("ldapgroupadd")
def do_ldap_group_add(self, subcmd, opts, groupname):
"""${cmd_name}: LDAP group add
${cmd_usage}
${cmd_option_list}
"""
try:
mothership.ldap.gadd(self.cfg, groupname)
except Exception, e:
print "Error: %s" % e
# remove a group from the ldap db
@cmdln.alias("ldgrm")
@cmdln.alias("ldgrouprm")
@cmdln.alias("ldgremove")
@cmdln.alias("ldgroupremove")
@cmdln.alias("ldapgurm")
@cmdln.alias("ldapgroupremove")
@cmdln.alias("ldap_grm")
def do_ldap_group_remove(self, subcmd, opts, groupname):
"""${cmd_name}: LDAP group remove
${cmd_usage}
${cmd_option_list}
"""
try:
mothership.ldap.gremove(self.cfg, groupname)
except Exception, e:
print "Error: %s" % e
# update a group's ldap entry
@cmdln.alias("ldgupdate")
@cmdln.alias("ldgu")
@cmdln.alias("ldgmodify")
@cmdln.alias("ldapgupdate")
@cmdln.alias("ldapgroupupdate")
@cmdln.alias("ldapgu")
def do_ldap_group_update(self, subcmd, opts, groupname):
"""${cmd_name}: LDAP group update
${cmd_usage}
${cmd_option_list}
"""
try:
mothership.ldap.gupdate(self.cfg, groupname)
except Exception, e:
print "Error: %s" % e
# display a group's ldap entry
@cmdln.alias("ldgd")
@cmdln.alias("ldapgdisplay")
@cmdln.alias("ldapgroupdisplay")
@cmdln.alias("ldapdisplaygroup")
@cmdln.alias("ldap_gdisplay")
def do_ldap_group_display(self, subcmd, opts, groupname):
"""${cmd_name}: LDAP group display
${cmd_usage}
${cmd_option_list}
"""
try:
mothership.ldap.gdisplay(self.cfg, groupname)
except Exception, e:
print "Error: %s" % e
# Server listing command
@cmdln.alias("lss")
@cmdln.alias("ls_serv")
@cmdln.alias("lsservers")
@cmdln.alias("ls_servers")
@cmdln.option("-c", "--config",
help="use db config file")
@cmdln.option("-v", "--verbose", action="store_true",
help="print verbose output")
@cmdln.option("-V", "--vlan", type="int",
help="print servers in one vlan")
@cmdln.option("-s", "--site-id",
help="print servers in one site")
@cmdln.option("-t", "--tag",
help="print servers in one tag")
@cmdln.option("-R", "--realm",
help="print servers in one realm")
@cmdln.option("-m", "--manufacturer",
help="print servers by one manufacturer")
@cmdln.option("-M", "--model",
help="print servers of one model type")
@cmdln.option("-C", "--cores",
help="print servers with a specific number of cpu cores")
@cmdln.option("-a", "--ram", type="int",
help="print servers with a specific amount of ram")
@cmdln.option("-d", "--disk", type="int",
help="print servers with a specific amount of disk")
@cmdln.option("-H", "--hw_tag",
help="print servers with a specific hw_tag")
@cmdln.option("-x", "--virtual", action="store_true",
help="print virtual servers")
@cmdln.option("-p", "--physical", action="store_true",
help="print physical (non-vm) servers")
@cmdln.option("-n", "--name",
help="print servers whose name matches *NAME*")
def do_list_servers(self, subcmd, opts):
"""${cmd_name}: get a list of all of the servers in mothership
${cmd_usage}
${cmd_option_list}
"""
if opts.vlan:
list_servers(self.cfg, listby='vlan', lookfor=opts.vlan)
elif opts.site_id:
list_servers(self.cfg, listby='site_id', lookfor=opts.site_id)
elif opts.tag:
list_servers(self.cfg, listby='tag', lookfor=opts.tag)
elif opts.realm:
list_servers(self.cfg, listby='realm', lookfor=opts.realm)
elif opts.manufacturer:
list_servers(self.cfg, listby='manufacturer', lookfor=opts.manufacturer)
elif opts.model:
list_servers(self.cfg, listby='model', lookfor=opts.model)
elif opts.cores:
list_servers(self.cfg, listby='cores', lookfor=opts.cores)
elif opts.ram:
list_servers(self.cfg, listby='ram', lookfor=opts.ram)
elif opts.disk:
list_servers(self.cfg, listby='disk', lookfor=opts.disk)
elif opts.hw_tag:
list_servers(self.cfg, listby='hw_tag', lookfor=opts.hw_tag)
elif opts.virtual:
list_servers(self.cfg, listby='virtual')
elif opts.physical:
list_servers(self.cfg, listby='physical')
elif opts.name:
list_servers(self.cfg, listby='name', lookfor=opts.name)
else:
list_servers(self.cfg)
@cmdln.alias("sinfo")
@cmdln.alias("si")
@cmdln.alias("server_info")
@cmdln.option("-i", "--ip",
help="search by ip")
@cmdln.option("-n", "--name",
help="search by hostname")
@cmdln.option("-H", "--hw_tag",
help="search by hardware tag")
@cmdln.option("-m", "--mac",
help="search by mac address")
@cmdln.option("-I", "--printip", action="store_true",
help="print ip addresses only")
@cmdln.option("-M", "--printmac", action="store_true",
help="print mac addresses only")
def do_serverinfo(self, subcmd, opts):
"""${cmd_name}: get information about a server
${cmd_usage}
${cmd_option_list}
"""
try:
arg_count = sum(x != None for x in (opts.ip, opts.name, opts.mac, opts.hw_tag))
if arg_count != 1:
print "Specify one search argument."
cmdln.Cmdln.do_help(self, ['','serverinfo'])
sys.exit(1)
if opts.ip:
hostname = mothership.serverinfo.get_host(self.cfg, ip=opts.ip)
elif opts.name:
hostname = opts.name
elif opts.mac:
hostname = mothership.serverinfo.get_host(self.cfg, mac=opts.mac)
elif opts.hw_tag:
hostname = mothership.serverinfo.get_host(self.cfg, hw_tag=opts.hw_tag)
else:
print "unexpected error, check everything."
sys.exit(1)
host, realm, site_id = mothership.get_unqdn(self.cfg, hostname)
if opts.printip and opts.printmac:
print "-M and -I options are mutually exclusive, pick one."
cmdln.Cmdln.do_help(self, ['','serverinfo'])
sys.exit(1)
if opts.printip:
mothership.serverinfo.ip_only(self.cfg, host, realm, site_id)
elif opts.printmac:
mothership.serverinfo.mac_only(self.cfg, host, realm, site_id)
else:
mothership.serverinfo.all(self.cfg, host, realm, site_id)
except Exception, e:
print 'Error: %s' % e
sys.exit(1)
@cmdln.alias("kv")
@cmdln.option("-u", "--update", action="store_true",
help="Update the value of the key. Will overwrite any existing values.")
@cmdln.option("-r", "--remove", action="store_true",
help="Delete a key=value pair.")
@cmdln.option("-a", "--add", action="store_true",
help="Add a key=value pair.")
def do_keyvalues(self, subcmd, opts, name, key):
"""${cmd_name}: Manipulate the kv table.
${cmd_option_list}
examples:
select: ship kv host.realm.site key # specific key for host.realm.site
ship kv realm.site key # specific key for realm.site
ship kv site key # specific key for site
ship kv host.realm.site % # all keys for host.realm.site
ship kv realm.site % # all keys for realm.site
ship kv site % # all keys for site
ship kv . key # the . is global
update: ship kv -u . key=global
ship kv -u name key=specific
append: ship kv -a . key=global
ship kv -a name array=three
remove: ship kv -r name key=junk
"""
kvs = []
# Global
if name == '.':
fqdn = ''
# Wildcard
elif name == '%':
fqdn = None
# Parse name parts
else:
# this causes much grief.
# fqdn = ".".join(mothership.get_unqdn(self.cfg, name))
fqdn = name
# Parse key=value argument
value = None
if key.find('=') > 0:
key, value = key.split('=')
if opts.add and opts.remove or opts.add and opts.update or opts.remove and opts.update:
print "Incompatible options."
return
try:
if opts.add:
# Add another value for a key
kvs = [mothership.kv.add(self.cfg, fqdn, key, value)]
elif opts.update:
# Update or insert the value for a key
kvs = [mothership.kv.upsert(self.cfg, fqdn, key, value)]
elif opts.remove:
if key and value:
# Display the value
kvs = [mothership.kv.select(self.cfg, fqdn, key, value)]
# Delete the value
mothership.kv.delete(self.cfg, fqdn, key, value)
else:
print "Delete requires key=value parameter."
else: # everything else should be collect
if key == '%':
# Query all values for a host
kvs = mothership.kv.collect(self.cfg, fqdn)
else:
# Query all values for a key
kvs = mothership.kv.collect(self.cfg, fqdn, key=key, value=value)
if kvs:
for kv in kvs:
print kv
except Exception, e:
print 'Error: %s' % e
@cmdln.alias("p")
@cmdln.alias("puppet")
def do_classify(self, subcmd, opts, fqdn):
"""${cmd_name}: Puppet classifier yaml for a server
${cmd_usage}
${cmd_option_list}
"""
node = mothership.puppet.classify(self.cfg, fqdn)
print(yaml.dump(node, default_flow_style=False))
@cmdln.alias("genip")
@cmdln.alias("gen_ip")
@cmdln.option("-l", "--last",
help="specify last ip address to generate")
@cmdln.option("-c", "--count",
help="specify number of ip addresses to generate")
def do_generate_ipaddress(self, subcmd, opts, first):
"""${cmd_name}:
Generate ip addresses given the first and last in range;
convert mac addresses and discover vlan, interface, etc;
and insert them into the network table
${cmd_usage}
${cmd_option_list}
"""
host,realm,site_id = mothership.get_unqdn(self.cfg, '')
mothership.generate_ipaddress_range(self.cfg, first,
last=opts.last, count=opts.count, realm=realm, site_id=site_id)
@cmdln.alias("rm_dns")
@cmdln.alias("deldns")
@cmdln.alias("rmdns")
@cmdln.alias("del_dns")
def do_delete_dns(self, subcmd, opts, hostname):
"""${cmd_name}: Remove DNS records from the dns_addendum table
Assumes the current realm and site_id unless options are specified
${cmd_usage}
${cmd_option_list}
"""
host,realm,site_id = mothership.get_unqdn(self.cfg, hostname)
print 'Deleting %s.%s.%s from dns_addendum table' % (host, realm, site_id)
mothership.dns.update_table_dnsaddendum(self.cfg,
{ 'realm':realm, 'site_id':site_id, 'host':host },
delete=True)
@cmdln.alias("dns")
@cmdln.alias("adddns")
def do_add_dns(self, subcmd, opts, hostname, rec_type, target):
"""${cmd_name}: Add DNS records to the dns_addendum table.
HOSTNAME should be unqdn.
you must generate dns to have your updates take effect.
see "ship gendns -h"
${cmd_usage}
${cmd_option_list}
"""
host,realm,site_id = mothership.get_unqdn(self.cfg, hostname)
print 'Adding %s.%s.%s to dns_addendum table' % (host, realm, site_id)
mothership.dns.update_table_dnsaddendum(self.cfg,
{ 'realm':realm, 'site_id':site_id,
'host':host, 'target':target.rstrip('.'),
'record_type':rec_type.upper() })
@cmdln.alias("gendns")
@cmdln.alias("gen_dns")
@cmdln.option("-s", "--system", action="store_true",
help="sync system files/config after confirmation of changes")
@cmdln.option("-a", "--all", action="store_true",
help="generate ALL zone files defined in mothership.yaml")
@cmdln.option("-o", "--outdir",
help="output zone file to specified directory")
def do_generate_dns(self, subcmd, opts, domain=None):
"""${cmd_name}: Generate DNS entries from dns_addendum table.
${cmd_usage}
${cmd_option_list}
"""
if is_ship_allowed_to_run(self.cfg):
if not domain and not opts.all:
cmdln.Cmdln.do_help(self, ['', subcmd])
return
if opts.system and os.getuid() > 0:
print "You are not privileged to generate system DNS! Please sudo"
return
mothership.dns.generate_dns_output(self.cfg, domain, opts)
else:
print 'dns generation in mothership has been time locked. please wait until the blackout window has ended.'
@cmdln.alias("add")
@cmdln.alias("add_serv")
@cmdln.alias("add_server")
@cmdln.option("-o", "--osname",
help="os or profile name for hostname (default: CentOS 5.5)")