Source

BlingNode / tools_init.py

Full commit
  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
from __future__ import with_statement # For python 2.5

import os
from os.path import join
import re

try:
	import common
	common = __import__('common')
except:	
	import initial_preperation
	initial_preperation.install_requirements()
	import common
	common = __import__('common')

import htpasswd_users
import subprocess
import shlex
from subprocess import CalledProcessError, PIPE, Popen
from optparse import OptionParser, make_option
from utils import list_callback

############################# Mercurial repo directory
HG_REPO="/var/lib/hg/"#+PROJECT_NAME

############################# Subversion repo directory
SVN_REPO="/var/lib/svn/"#+PROJECT_NAME

############################# {OpenGrok data directory
OPENGROK="/var/lib/opengrok/"#+PROJECT_NAME

############################# Trac env directory
TRAC_ENV="/var/lib/trac/"#+PROJECT_NAME

############################# AjaXplorer file storage directory
FILE_STORAGE="/var/lib/files/"#+PROJECT_NAME

############################# Options
TRAC_ENV_WANTED=""
HG_REPO_WANTED=""
SVN_REPO_WANTED=""
OPENGROK_WANTED=""
FILE_STORAGE_WANTED=""

############################# Repo choice
REPO=""

def mercurial_repo_setup(project_name, domain_name, sudo=None, repo=None):
    """
        Sets up the Mercurial repository for the site
    """
    global HG_REPO, HG_REPO_WANTED
    #append project name
    HG_REPO =HG_REPO+project_name

    if repo == "hg":
        HG_REPO_WANTED = "y"
    else:
        HG_REPO_WANTED =common.ask_yes_or_no("\n\nWould you like to create a Mercurial repo? (y/n)")

    cgi_file ="/var/lib/hg/"+project_name+".cgi"

    if HG_REPO_WANTED == "y":
    # repo is wanted so make it

        def to_do():
            hg_init_dir = '/var/lib/misc/blingnode/lib/mercurial-init-collection'

            print "\n---- Making Mercurial repo ----"

            if sudo:
                sudo.run("hg init %(repo)s" % {'repo': HG_REPO })

            else:
                common.command_line_call("""sudo hg init """+HG_REPO)

            print "-- Created Mercurial repo --"

            print "-- Creating new hgrc config file --"


            hgrc = join(hg_init_dir, 'hgrc')
            hgrc_dest = join(HG_REPO, '.hg', 'hgrc')

            # copy the blingnode template over to the given directory, and
            # replace the template variables PROJECT_NAME and DOMAIN_NAME
            # with the user supplied values. Note that this doesn't use
            # copy_and_sub, as that function doesn't easily support replacing
            # multiple values.
            source_text = ''

            with open(hgrc, 'r') as hgrc_source:
                source_text = hgrc_source.read()

            target_text = ''

            with open(hgrc_dest, 'w') as hgrc_dest:
                target_text = re.sub(r'PROJECT_NAME', project_name,
                                     source_text)

                target_text = re.sub(r'DOMAIN_NAME', domain_name, target_text)
                hgrc_dest.write(target_text)


            print "-- Now let apache own it --"

            if sudo:
                sudo.run("chown -R www-data:www-data %(repo)s" % {'repo': HG_REPO})
            else:
                common.command_line_call("""chown -R www-data:www-data """+HG_REPO)

            print "-- Also let apache own the .hg directory --"
            
            if sudo:
                sudo.run("chown -R www-data:www-data %(repo)s/.hg" % {'repo': HG_REPO})
                sudo.run("chown -R root:root %(repo)s/.hg/hgrc" % {'repo': HG_REPO})
            else:
                common.command_line_call("""sudo chown -R www-data:www-data """+HG_REPO+"""/.hg""")
                common.command_line_call("""sudo chown root:root """+HG_REPO+"""/.hg/hgrc""")


            print "-- Create necessary CGI file --"
            common.copy_and_sub(r"%(hg_init_dir)s/hgweb-template.cgi" % locals(),
                                cgi_file,
                                r"PROJECT_NAME",
                                project_name)

            print "---- Mercurial repo Created ----"

            common.check_for_htpasswd_file(project_name, sudo=sudo)

        if common.does_directory_exist(HG_REPO):
            #common.error_exit(msg="XXXX ---- you are going to overwrite "+HG_REPO+". try again. ---- XXXX")
            common.on_conflict("XXXX ---- you are going to overwrite "+HG_REPO+". ---- XXXX", to_do, HG_REPO)

        else:
            to_do()

def svn_repo_setup(project_name, sudo=None, repo=None):
    """
        Sets up the svn repository for this site
    """
    global SVN_REPO, SVN_REPO_WANTED

    if repo == "svn":
        SVN_REPO_WANTED = "y"
    else:
        SVN_REPO_WANTED = common.ask_yes_or_no(query="\nWould you like to create an svn repo? (y/n)")

    SVN_REPO = SVN_REPO + project_name

    if SVN_REPO_WANTED == "y":

        def to_do():
                
            print "\n---- Making svn repo ----"

            if sudo:
                sudo.run("svnadmin create %(repo)s" % {'repo': SVN_REPO})
            else:
                common.command_line_call("""sudo svnadmin create """+SVN_REPO)

            print "-- Create SVN repo --"
            print "-- Now let apache own it --"

            if sudo:
                sudo.run("chown -R www-data:www-data %(repo)s" % {'repo': SVN_REPO })
            else:
                common.command_line_call("""sudo chown -R www-data:www-data """+SVN_REPO)
            print "---- SVN repo Created ----"

        if common.does_directory_exist(SVN_REPO):
            #common.error_exit(msg="\nDirectory '"+SVN_REPO+"'exists. Exiting script to avoid overwriting the directory.")
            common.on_conflict("XXXX ---- you are going to overwrite "+SVN_REPO+". ---- XXXX", to_do, SVN_REPO)

        else:
            to_do()

def opengrok_setup(project_name, sudo=None, opengrok=False):
    """
        Sets up {OpenGrok for the site
    """
    global OPENGROK, OPENGROK_WANTED
    
    if opengrok == "yes":
        OPENGROK_WANTED = "y"
    elif opengrok == "no":
        OPENGROK_WANTED = "n"
    else:
        OPENGROK_WANTED = common.ask_yes_or_no("\n\nWould you like to setup Source Code Search with {OpenGrok? (y/n)")
    
    if OPENGROK_WANTED != "y":
        return
    
    if sudo:
        run_command = sudo.run
    else:
        run_command = common.command_line_call
    
    # Install opengrok dependencies
    run_command('aptitude update')
    
    machine_os = common.get_os()
    if machine_os == 'ubuntu-10':
        # Just install them, environment variables automatically set during package configuration
        run_command('aptitude install openjdk-6-jre tomcat6 exuberant-ctags')
        
        tomcat = 'tomcat6'
    elif machine_os == 'debian-5':
        install_cmd = 'aptitude install openjdk-6-jre tomcat5.5 exuberant-ctags'
        
        try:
            if sudo:
                sudo.run(install_cmd)
            else:
                subprocess.check_call(shlex.split(install_cmd))
        except subprocess.CalledProcessError:
            print "\nError encountered, assuming it's because JAVA_HOME is not set for tomcat5.5"
        
        # Edit /etc/defaults/tomcat5.5 to put in JAVA_HOME (if needed)
        run_command('sed -i "s/#JAVA_HOME=.*$/JAVA_HOME=\/usr\/lib\/jvm\/java-6-openjdk\/jre/" /etc/default/tomcat5.5')
        
        # Disable tomcat security because it was disallowing opengrok from accessing something related to ctags
        run_command('sed -i "s/.*TOMCAT5_SECURITY=yes/TOMCAT5_SECURITY=no/" /etc/default/tomcat5.5')
        
        os.putenv('JAVA_HOME', '/usr/lib/jvm/java-6-openjdk/jre')
        
        # Finish configuring tomcat5.5 (which should start it) if ever it
        # didn't finish successfully because JAVA_HOME wasn't yet set
        try:
            if sudo:
                sudo.run('dpkg --configure tomcat5.5')
            else:
                subprocess.check_call(shlex.split('dpkg --configure tomcat5.5'))
        except subprocess.CalledProcessError:
            print "\nError encountered, assuming it's because tomcat5.5 was already successfully configured"
        
        tomcat = 'tomcat5.5'
    
    opengrok_source_dir = os.path.join(OPENGROK, "source")
    
    opengrok_index_cmd = "/var/lib/misc/blingnode/lib/opengrok-init-collection/opengrok/bin/OpenGrok index '%s'" % opengrok_source_dir
    if REPO == 'svn':
        repo_dir = SVN_REPO
    elif REPO == 'hg':
        repo_dir = HG_REPO
    
    # Make opengrok source dir (if necessary) and put symlink to project repo there
    run_command("mkdir -p %s" % opengrok_source_dir)
    run_command("ln -fs %s %s" % (repo_dir, opengrok_source_dir))
    
    # Deploy opengrok webapp
    if not os.path.exists('/var/lib/%s/webapps/source' % tomcat):
        run_command("cp -r /var/lib/misc/blingnode/lib/opengrok-init-collection/opengrok/lib/source /var/lib/%s/webapps/" % tomcat)
        run_command("/etc/init.d/%s restart" % tomcat)
    
    # Index new repo
    os.putenv('OPENGROK_INSTANCE_BASE', OPENGROK)
    run_command(opengrok_index_cmd)
    
    run_command("chmod -R 757 %s/data" % OPENGROK)
    run_command("chmod -R 757 %s/etc" % OPENGROK)
    run_command("chmod -R 757 %s/log" % OPENGROK)
    
def trac_env_setup(project_name, sudo=None, trac=False, admins=None):
    """
        Sets up the Trac environment for the site
    """
    global TRAC_ENV, TRAC_ENV_WANTED
    #append project name
    TRAC_ENV =TRAC_ENV+project_name

    def set_trac_env_permissions():
        dir_contents = os.listdir(TRAC_ENV)

        if sudo:
            for content in dir_contents:
                sudo.run("chown -R www-data:www-data %(trac_path)s" %
                         {'trac_path': os.path.join(TRAC_ENV, content)})
        else:
            for content in dir_contents:
                #print "setting permission for: "+content
                common.command_line_call("""chown -R www-data:www-data """+TRAC_ENV+"""/"""+content)

    # -- location of all trac plugins > /usr/share/trac/global_plugins
    # -- as long as they are defined in trac.ini, and installed in global_plugins, we will be good

    if trac == "yes":
        TRAC_ENV_WANTED = "y"
    elif trac == "no":
        TRAC_ENV_WANTED = "n"
    else:
        TRAC_ENV_WANTED = common.ask_yes_or_no("\n\nWould you like to make a Trac Environment? (y/n)")

    if TRAC_ENV_WANTED == "y":
        def to_do():

            def setup_trac_repo():
                """
                    Sets up the repo that trac will use, depending on the user's chosen repo
                    Skips if 'none'/no repo was selected
                """
                global REPO, SVN_REPO, TRAC_ENV

                if REPO=="hg":
                    #---------- Begin hg here document ------- #
                    trac_setup_commands=project_name+"\n\nhg\n\n"
                    trac_command_string = "trac-admin "+TRAC_ENV+" initenv"
                    trac_command_list   = trac_command_string.split(" ")

                    trac_output = Popen(trac_command_list,stdin=PIPE,stdout=PIPE).communicate(input=trac_setup_commands)[0]

                    #write output to /var/lib/misc/blingnode/script-output/
                    common.write_to_script_output("%s_trac_output.txt" % project_name, trac_output)

                    #---------- End hg here document ------- #
                elif REPO=="svn":
                    #---------- Begin svn here document ------- #
                    trac_setup_commands=project_name+"\n\nsvn\n"+SVN_REPO
                    trac_command_string = "trac-admin "+TRAC_ENV+" initenv"
                    trac_command_list   = trac_command_string.split(" ")

                    trac_output = Popen(trac_command_list,stdin=PIPE,stdout=PIPE).communicate(input=trac_setup_commands)[0]

                    #write output to /var/lib/misc/blingnode/script-output/
                    common.write_to_script_output("%s_trac_output.txt" % project_name, trac_output)
                    #---------- End svn here document ------- #
                elif REPO=="none":
                    # No repo still requires trac setup.
                    # For trac to not use a repo, use the default VCS (svn) and don't specify a path.
                    #---------- Begin no-repo here document ------- #
                    trac_setup_commands=project_name+"\n\n\n\n"
                    trac_command_string = "trac-admin "+TRAC_ENV+" initenv"
                    trac_command_list   = trac_command_string.split(" ")

                    trac_output = Popen(trac_command_list,stdin=PIPE,stdout=PIPE).communicate(input=trac_setup_commands)[0]

                    #write output to /var/lib/misc/blingnode/script-output/
                    common.write_to_script_output("%s_trac_output.txt" % project_name, trac_output)

                    #---------- End no-repo here document ------- #
                    print "\nNo repo has been setup, skipping trac repo setup.\n"
                else:
                    common.error_exit(msg="Invalid repo choice!")

            print "\n---- Making Trac Environment ----"

            # creating trac env

            setup_trac_repo()

            # importing missing files
            if sudo:
                for directory in ('conf', 'db', 'htdocs', '.egg-cache'):
                    full_path = os.path.join(TRAC_ENV, directory)

                    if not common.does_directory_exist(full_path):
                        sudo.run('mkdir %s' % full_path)
            else:
                for directory in ('conf', 'db', 'htdocs', '.egg-cache'):
                    full_path = os.path.join(TRAC_ENV, directory)

                    if not common.does_directory_exist(full_path):
                        common.command_line_call("sudo mkdir %s" % full_path)


            trac_init = '/var/lib/misc/blingnode/lib/trac-init-collection'

            if sudo:
                sudo.run("sudo cp -RfT %s/db/trac.db %s/db/trac.db" % (trac_init, TRAC_ENV))
                sudo.run("sudo cp -RfT %s/htdocs %s/htdocs" % (trac_init, TRAC_ENV))
            else:
                common.command_line_call("sudo cp -RfT %s/db/trac.db %s/db/trac.db" % (trac_init, TRAC_ENV))
                common.command_line_call("sudo cp -RfT %s/htdocs %s/htdocs" % (trac_init, TRAC_ENV))

            print "-- Missing Files Imported --"

            # we already have replaced trac.ini, now fix it
            print "-- Replacing trac.ini --"

            common.copy_and_sub("/var/lib/misc/blingnode/lib/trac-init-collection/conf/trac-master.ini",TRAC_ENV+"/conf/trac-master.ini","PROJECT_NAME",project_name,overwrite=True)

            if REPO == "hg":
                common.copy_and_sub("/var/lib/misc/blingnode/lib/trac-init-collection/conf/trac.ini.hg",TRAC_ENV+"/conf/trac.ini","PROJECT_NAME",project_name,overwrite=True)
            elif REPO == "svn":
                common.copy_and_sub("/var/lib/misc/blingnode/lib/trac-init-collection/conf/trac.ini.svn",TRAC_ENV+"/conf/trac.ini","PROJECT_NAME",project_name,overwrite=True)
            else:
                common.copy_and_sub("/var/lib/misc/blingnode/lib/trac-init-collection/conf/trac.ini.none",TRAC_ENV+"/conf/trac.ini","PROJECT_NAME",project_name,overwrite=True)

            #Give it the global plugins
            print "-- Global plugins linked --"

            if sudo:
                plugin_path = os.path.join(TRAC_ENV, 'plugins')

                sudo.run('rm -rf %s' % plugin_path)
                sudo.run('ln -s /usr/share/trac/global_plugins %s' % plugin_path)

            else:
                common.command_line_call("""rm -rf """+TRAC_ENV+"""/plugins""")
                common.command_line_call("""ln -s /usr/share/trac/global_plugins """+TRAC_ENV+"""/plugins""")
            
            plugin_cachedir_command = 'mkdir %s' % os.path.join(TRAC_ENV, 'gvcache')
            if sudo:
                sudo.run(plugin_cachedir_command)
            else:
                common.command_line_call(plugin_cachedir_command)
            print '-- Plugin cache directory created --'

            # set proper perms
            print "-- Proper permissions set --"
            set_trac_env_permissions()

            # upgrade needed with plugins from our ini
            print "-- Trac upgraded --"

            if sudo:
                sudo.run('trac-admin %s upgrade' % TRAC_ENV)
                sudo.run('trac-admin %s resync' % TRAC_ENV)

            else:
                common.command_line_call_and_return(r"""sudo trac-admin """+TRAC_ENV+r""" upgrade""", suppress_warnings=True)
                # resync for changed repository_type
                common.command_line_call_and_return(r"""sudo trac-admin """+TRAC_ENV+r""" resync""", suppress_warnings=True)

            common.check_for_htpasswd_file(project_name)

            # ask for trac admins
            #print "adding track admins"
            if admins:
                add_trac_admins_now = "y"
            else:
                add_trac_admins_now = common.ask_yes_or_no("\nAdd trac admins now? (y/N)")
            
            if add_trac_admins_now == "y":
                htpasswd_re = r'(?P<name>[A-Za-z0-9\_]+):.+'
                htpasswd_file = open('/var/lib/htpasswd/%s.htpasswd' % project_name, 'rb')
                htpasswd_users = []
                trac_users = []

                for line in htpasswd_file:
                    match = re.search(htpasswd_re, line)
                    htpasswd_users.append(match.group('name'))

                htpasswd_file.close()

                print "\n-- Adding trac admins --"
                print "Available users:"

                for user in htpasswd_users:
                    print " - %s" % user
                
                if not admins:
                    while True:
                        if len(trac_users) == len(htpasswd_users):
                            print "WARNING: All users in the HTPASSWD file are already Trac Admins."

                        selected = raw_input('\nEnter name of user or leave blank to move on: ')
                        if not selected:
                            break

                        if selected in trac_users:
                            print "User %s is already a Trac admin!" % selected
                            continue

                        if selected in htpasswd_users:
                            common.command_line_call_and_return("""sudo trac-admin %s permission add %s TRAC_ADMIN""" % (TRAC_ENV, selected),suppress_warnings=True)
                            trac_users.append(selected)
                        else:
                            print '\nThere is no user named %s in the list!' % selected
                            force_admin = common.ask_yes_or_no("Are you sure you want to add '%s' as an Admin? (y/n)" % selected)
                            if force_admin == "y":
                                common.command_line_call("""sudo trac-admin %s permission add %s TRAC_ADMIN""" % (TRAC_ENV, selected))
                else:
                    if len(trac_users) == len(htpasswd_users):
                        print "WARNING: All users in the HTPASSWD file are already Trac Admins."
                    for admin in admins:
                        selected = admin[0]
                        if selected in trac_users:
                            print "User %s is already a Trac admin!" % selected
                            continue
                        
                        if selected in htpasswd_users:
                            common.command_line_call_and_return("""sudo trac-admin %s permission add %s TRAC_ADMIN""" % (TRAC_ENV, selected),suppress_warnings=True)
                            trac_users.append(selected)
                        else:
                            print '\nThere is no user named %s in the list! Skipping.' % selected

            print "\n---- Trac env setup complete ----"

            set_trac_env_permissions()

        if common.does_directory_exist(TRAC_ENV):
            #common.error_exit(msg="XXXX ---- you are going to overwrite "+TRAC_ENV+". try again. ---- XXXX")
            common.on_conflict("XXXX ---- you are going to overwrite "+TRAC_ENV+". ---- XXXX", to_do, TRAC_ENV)
        else:
            to_do()

def ajaxplorer_setup(project_name, sudo=None, storage=False):
    """
        Sets up the AjaXplorer file storage for the site
    """

    # -- location of AjaXplorer codebase /var/lib/misc/blingnode/lib/ajaxplorer-init-collection
    # -- Location of apache template file is /var/lib/misc/blingnode/lib/ajaxplorer-init-collection/templates/apache-template
    global FILE_STORAGE, FILE_STORAGE_WANTED
    #append project name
    FILE_STORAGE =FILE_STORAGE+project_name

    if storage == "yes":
        FILE_STORAGE_WANTED="y"
    elif storage == "no":
        FILE_STORAGE_WANTED="n"
    else:
        FILE_STORAGE_WANTED=common.ask_yes_or_no(query="\n\nWould you like to make a File Storage with AjaXplorer? (y/n)")

    if FILE_STORAGE_WANTED == "y":
        def to_do():
            print "\n---- Making File Storage ----"

            #### creating File Storage ####

            # make folder
            common.command_line_call("""mkdir """+FILE_STORAGE)

            # copy over AjaXplorer
            common.command_line_call("""cp -r /var/lib/misc/blingnode/lib/ajaxplorer-init-collection/AjaXplorer-3.2.3 """+FILE_STORAGE+"""/""")

            # chown folder to www-data
            common.command_line_call("""sudo chown -R www-data:www-data """+FILE_STORAGE)

            print "\n---- File Storage setup complete ----"

            common.check_for_htpasswd_file(project_name)

        if common.does_directory_exist(FILE_STORAGE):
            #common.error_exit(msg="XXXX ---- you are going to overwrite "+FILE_STORAGE+". try again. ---- XXXX")
            common.on_conflict("XXXX ---- you are going to overwrite "+FILE_STORAGE+". ---- XXXX",to_do, FILE_STORAGE)

        else:
            to_do()


def apache_hg_trac_setup(project_name, domain_name, sudo=None):

    ############################# Only Create Sites-Available file if the User Created an TRAC ENVIRONMENT earlier
    global TRAC_ENV_WANTED
    if TRAC_ENV_WANTED == "y":

        trac_apache_site_available_file="trac."+project_name+"."+domain_name
        trac_apache_site_available="/etc/apache2/sites-available/"+trac_apache_site_available_file

        def to_do():
            print "\n---- Making TRAC Apache Site ----"

            # change values
            common.copy_and_sub("/var/lib/misc/blingnode/lib/apache-files-collection/trac_hg_template", trac_apache_site_available + "_", "PROJECT_NAME", project_name)
            common.copy_and_sub(trac_apache_site_available + "_", trac_apache_site_available, "DOMAIN", domain_name)

            if sudo:
                sudo.run('rm -rf %s_' % trac_apache_site_available)

            else:
                common.command_line_call("sudo rm -f %s_" % trac_apache_site_available)

            # enable site
            print "-- Going to Enable "+trac_apache_site_available_file+" --"

            if sudo:
                sudo.run('a2ensite %s' % trac_apache_site_available_file)
                sudo.run('/etc/init.d/apache2 reload')

            else:
                common.command_line_call("""a2ensite """+trac_apache_site_available_file)
                # restart apache
                common.command_line_call("""/etc/init.d/apache2 reload """)

            # tell user what to setup DNS
            print "---- Success: Apache Trac site setup ----"

        if common.does_file_exist(trac_apache_site_available):
            #common.error_exit(msg="XXXX ---- you are going to overwrite "+trac_apache_site_available+". try again. ---- XXXX")
            common.on_conflict("XXXX ---- you are going to overwrite "+trac_apache_site_available+". try again. ---- XXXX",to_do,trac_apache_site_available)
        else:
            to_do()

    else:
        print "\n---- You did NOT create a TRAC ENVIRONMENT ----"

def apache_svn_trac_setup(project_name, domain_name):

    ############################# Only Create Sites-Available file if the User Created an TRAC ENVIRONMENT earlier
    global TRAC_ENV_WANTED
    if TRAC_ENV_WANTED == "y":

        trac_apache_site_available_file="trac."+project_name+"."+domain_name
        trac_apache_site_available="/etc/apache2/sites-available/"+trac_apache_site_available_file

        def to_do():
            print "\n---- Making TRAC Apache Site ----"

            # change values
            common.copy_and_sub("/var/lib/misc/blingnode/lib/apache-files-collection/trac_svn_template", trac_apache_site_available + "_", "PROJECT_NAME", project_name)
            common.copy_and_sub(trac_apache_site_available + "_", trac_apache_site_available, "DOMAIN", domain_name)
            common.command_line_call("sudo rm -f %s_" % trac_apache_site_available)

            # enable site
            print "-- Going to Enable "+trac_apache_site_available_file+" --"
            common.command_line_call("""a2ensite """+trac_apache_site_available_file)

            # restart apache
            common.command_line_call("""/etc/init.d/apache2 reload """)

            # tell user what to setup DNS
            print "---- Success: Apache Trac site setup ----"

        if common.does_file_exist(trac_apache_site_available):
            #common.error_exit(msg="XXXX ---- you are going to overwrite "+trac_apache_site_available+". try again. ---- XXXX")
            common.on_conflict("XXXX ---- you are going to overwrite "+trac_apache_site_available+". try again. ---- XXXX",to_do,trac_apache_site_available)
        else:
            to_do()

    else:
        print "\n---- You did NOT create a TRAC ENVIRONMENT ----"

def apache_mercurial_setup(project_name, domain_name):
    ############################# Only Create Sites-Available file if the User Created an Mercurial Repo Earlier
    global HG_REPO_WANTED
    if HG_REPO_WANTED == "y":

        hg_apache_site_available_file="source."+project_name+"."+domain_name
        hg_apache_site_available="/etc/apache2/sites-available/"+hg_apache_site_available_file

        def to_do():
            print "\n---- Making HG Apache Site ----"

            # change values
            common.copy_and_sub("/var/lib/misc/blingnode/lib/apache-files-collection/source_hg_template", hg_apache_site_available + "_", "PROJECT_NAME", project_name)
            common.copy_and_sub(hg_apache_site_available + "_", hg_apache_site_available, "DOMAIN", domain_name)
            common.command_line_call("sudo rm -f %s_" % hg_apache_site_available)

            # enable site
            print "-- Going to Enable "+hg_apache_site_available_file+" --"
            common.command_line_call("""a2ensite """+hg_apache_site_available_file)

            # restart apache
            common.command_line_call("""/etc/init.d/apache2 reload""")

            # tell user what to setup DNS
            print "---- Success: Apache Mercurial site setup ----"

        if common.does_file_exist(hg_apache_site_available):
            #common.error_exit(msg="XXXX ---- you are going to overwrite "+hg_apache_site_available_file+". try again. ---- XXXX")
            common.on_conflict("XXXX ---- you are going to overwrite "+hg_apache_site_available+". ---- XXXX",to_do, hg_apache_site_available)
        else:
            to_do()

def apache_svn_setup(project_name, domain_name):
    ############################# Only Create Sites-Available file if the User Created an Mercurial Repo Earlier
    global SVN_REPO_WANTED
    if SVN_REPO_WANTED == "y":

        svn_apache_site_available_file="source."+project_name+"."+domain_name
        svn_apache_site_available="/etc/apache2/sites-available/"+svn_apache_site_available_file

        def to_do():
            print "\n---- Making SVN Apache Site ----"

            # change values
            common.copy_and_sub("/var/lib/misc/blingnode/lib/apache-files-collection/source_svn_template", svn_apache_site_available + "_", "PROJECT_NAME", project_name)
            common.copy_and_sub(svn_apache_site_available + "_", svn_apache_site_available, "DOMAIN", domain_name)
            common.command_line_call("sudo rm -f %s_" % svn_apache_site_available)

            # enable site
            print "-- Going to Enable "+svn_apache_site_available_file+" --"
            common.command_line_call("""a2ensite """+svn_apache_site_available_file)

            # restart apache
            common.command_line_call("""/etc/init.d/apache2 reload""")

            # tell user what to setup DNS
            print "---- Success: Apache Mercurial site setup ----"

        if common.does_file_exist(svn_apache_site_available):
            #common.error_exit(msg="XXXX ---- you are going to overwrite "+svn_apache_site_available_file+". try again. ---- XXXX")
            common.on_conflict("XXXX ---- you are going to overwrite "+svn_apache_site_available+". ---- XXXX",to_do, svn_apache_site_available)
        else:
            to_do()

def apache_hg_file_storage_setup(project_name, domain_name):
    ############################# Only Create Sites-Available file if the User Created a FILE STORAGE  Earlier
    global FILE_STORAGE_WANTED
    if FILE_STORAGE_WANTED == "y":

        file_storage_apache_site_available_file="files."+project_name+"."+domain_name
        file_storage_apache_site_available="/etc/apache2/sites-available/"+file_storage_apache_site_available_file

        def to_do():
            print "\n---- Making FILE STORAGE Apache Site ----"

            # change values
            common.copy_and_sub("/var/lib/misc/blingnode/lib/apache-files-collection/files_hg_template", file_storage_apache_site_available + "_", "PROJECT_NAME", project_name)
            common.copy_and_sub(file_storage_apache_site_available + "_", file_storage_apache_site_available, "DOMAIN", domain_name)
            common.command_line_call("sudo rm -f %s_" % file_storage_apache_site_available)

            # enable site
            print "-- Going to Enable "+file_storage_apache_site_available+" --"
            common.command_line_call("""a2ensite """+file_storage_apache_site_available_file)

            # restart apache
            common.command_line_call("""/etc/init.d/apache2 reload """)

            # tell user what to setup DNS
            print "---- Success: Apache File site setup. ----"

        if common.does_file_exist(file_storage_apache_site_available):
            #common.error_exit(msg="XXXX ---- you are going to overwrite "+file_storage_apache_site_available+". try again. ---- XXXX")
            common.on_conflict("XXXX ---- you are going to overwrite "+file_storage_apache_site_available+". ---- XXXX",\
to_do, file_storage_apache_site_available)

        else:
            to_do()
    else:
        print "\n---- You did NOT create a FILE STORAGE ----"

def apache_svn_file_storage_setup(project_name, domain_name):
    ############################# Only Create Sites-Available file if the User Created a FILE STORAGE  Earlier
    global FILE_STORAGE_WANTED
    if FILE_STORAGE_WANTED == "y":

        file_storage_apache_site_available_file="files."+project_name+"."+domain_name
        file_storage_apache_site_available="/etc/apache2/sites-available/"+file_storage_apache_site_available_file

        def to_do():
            print "\n---- Making FILE STORAGE Apache Site ----"

            # change values
            common.copy_and_sub("/var/lib/misc/blingnode/lib/apache-files-collection/files_svn_template", file_storage_apache_site_available + "_", "PROJECT_NAME", project_name)
            common.copy_and_sub(file_storage_apache_site_available + "_", file_storage_apache_site_available, "DOMAIN", domain_name)
            common.command_line_call("sudo rm -f %s_" % file_storage_apache_site_available)

            # enable site
            print "-- Going to Enable "+file_storage_apache_site_available+" --"
            common.command_line_call("""a2ensite """+file_storage_apache_site_available_file)

            # restart apache
            common.command_line_call("""/etc/init.d/apache2 reload """)

            # tell user what to setup DNS
            print "---- Success: Apache File site setup. ----"

        if common.does_file_exist(file_storage_apache_site_available):
            #common.error_exit(msg="XXXX ---- you are going to overwrite "+file_storage_apache_site_available+". try again. ---- XXXX")
            common.on_conflict("XXXX ---- you are going to overwrite "+file_storage_apache_site_available+". ---- XXXX",\
to_do, file_storage_apache_site_available)

        else:
            to_do()
    else:
        print "\n---- You did NOT create a FILE STORAGE ----"

def repo_query():
    while True:
        print "\nWill you use Subversion (svn) or Mercurial (hg) for the site's repository?"
        repo = raw_input("\nEnter repository to be used (svn/hg/none):\n")
        repo = repo.lower()
        if repo=="svn" or repo=="hg" or repo=="none":
            return repo
        else:
            print "\nYou entered an invalid repository. Try again."

def main(options, args):
    ####### Check For root Access
    if not common.is_user_root():
        common.error_exit(msg="\nERROR: Run this script as root\n")

    sudo = None

    if options.password:
        print "password: %s" % options.password
        sudo = common.Sudo(password = options.password)

    #tell user what we will accomplish
    print "\n\n---- You are setting up a new Collaboration Tools Env.  You will be guided through the following: ----"
    print "---- Mercurial setup/Trac setup/File Storage setup/Apache setup/ ----"

    # show user variables to fill
    if hasattr(options,"project") and options.project:
        project_name = options.project
    else:
        project_name = raw_input("\nWhat is the Project Name?\n")
    if hasattr(options,"domain") and options.domain:
        domain_name = options.domain
    else:
        domain_name = raw_input("\nWhat domain name will this project be in?\n")

    ####### htpasswd setup
    if hasattr(options,"htpasswd_users") and options.htpasswd_users:
        htpasswd_users.run_with_no_options(sudo, project_name, users=options.htpasswd_users)
    else:
        htpasswd_users.run_with_no_options(sudo, project_name)
        
    global REPO
    #ask user what repository to use
    if hasattr(options,"repo") and options.repo:
        REPO = options.repo
    else:
        REPO = repo_query()

    if REPO=="hg":
        ####### Mercurial repo setup
        print "Mercurial repo selected"
        mercurial_repo_setup(project_name, domain_name, sudo=sudo, repo=REPO)

    elif REPO=="svn":
        print "SVN repo selected"
        ####### SVN repo setup
        svn_repo_setup(project_name, sudo=sudo, repo=REPO)
    elif REPO=="none":
        print "\nNo repository will be set up.\n"
    else:
        common.error_exit(msg="Invalid repo choice %s" % REPO)
    
    ####### {OpenGrok setup
    if REPO=="none":
        print "\nNo repository set up, skipping {OpenGrok setup"
    elif hasattr(options,"opengrok") and options.opengrok:
        opengrok_setup(project_name, sudo=sudo, opengrok=options.opengrok)
    else:
        opengrok_setup(project_name, sudo=sudo)

    ####### Trac env setup
    if hasattr(options,"trac") and options.trac:
        if hasattr(options,"trac_admins") and options.trac_admins:
            trac_env_setup(project_name, sudo=sudo, trac=options.trac, admins=options.trac_admins)
        else:
            trac_env_setup(project_name, sudo=sudo, trac=options.trac)
    else:
        trac_env_setup(project_name, sudo=sudo)

    ####### AjaXplorer setup
    if hasattr(options,"storage") and options.storage:
        ajaxplorer_setup(project_name, sudo=sudo, storage=options.storage)
    else:
        ajaxplorer_setup(project_name, sudo=sudo)

    print "\n\n\n---- Creating Sites-Available files for Trac and Mercurial ----"
    
    if REPO=="hg":
        ####### apache hg trac setup
        apache_hg_trac_setup(project_name, domain_name)

        ####### apache Mercurial setup
        apache_mercurial_setup(project_name, domain_name)

        ####### apache hg file storage setup
        apache_hg_file_storage_setup(project_name, domain_name)

    elif REPO=="svn":
        ####### apache svn trac setup
        apache_svn_trac_setup(project_name, domain_name)

        ####### apache  Subversion setup
        apache_svn_setup(project_name, domain_name)

        ####### apache svn file storage setup
        apache_svn_file_storage_setup(project_name, domain_name)
        
    elif REPO=="none":
        print "\nNo repository will be set up.\n"
        print "sites-available files for trac and file storage will"
        print "  be derived from apache hg templates.\n"

        apache_hg_trac_setup(project_name, domain_name)
        apache_hg_file_storage_setup(project_name, domain_name)
        
    else:
        common.error_exit(msg="Invalid repo choice %s" % REPO)

    print "\n\n!!!! SUCCESS !!!!\n\n\n ---- Now you need to: ----\n1. Make DNS Entries in your DNS Manager\n2. Maybe run the django_ve_init.py file to create a new Development Env\n3. Start Using your new Project Environment!"

if __name__ == "__main__":
    options = [
        make_option('-p', '--password', dest='password', action='store',
            help="User password to send to sudo. Note that this will"
            "appear in the operating system's process list."),
        make_option('--project', dest='project',
            help="Project name"),
        make_option('--domain', dest='domain',
            help="Domain name that the project will be in"),
        make_option('--repo', dest='repo',
            help="The repository to be used (svn/hg/none)"),
        make_option('--opengrok', dest='opengrok', action='store', default=False,
            help="sets up source code search with {OpenGrok (ignored when no repo used)"),
        make_option('--trac', dest='trac', action='store', default=False,
            help="sets up Trac Environment"),
        make_option('--storage', dest='storage', action='store', default=False,
            help="sets up File Storage with AjaXplorer"),
        make_option('--htpasswd_users', type='string', action='callback', callback=list_callback,
            help="accepts a list of usernames and passwords to be included in the .htpasswd file. sample input: username1:password1,username2:password2"),
        make_option('--trac_admins', type='string', action='callback', callback=list_callback,
            help="accepts a list of usernames to be added to the trac admin. sample input: username1,username2"),
    ]

    parser = OptionParser(option_list=options)
    (options, args) = parser.parse_args()

    main(options, args)