]> git.rkrishnan.org Git - tahoe-lafs/tahoe-lafs.git/blob - src/allmydata/scripts/create_node.py
cli: merge the better version of David-Sarah's split-usage-and-help patch with the...
[tahoe-lafs/tahoe-lafs.git] / src / allmydata / scripts / create_node.py
1
2 import os, sys
3 from twisted.python import usage
4 from allmydata.scripts.common import BasedirMixin, NoDefaultBasedirMixin
5
6 class CreateClientOptions(BasedirMixin, usage.Options):
7     optParameters = [
8         ("basedir", "C", None, "which directory to create the node in"),
9         # we provide 'create-node'-time options for the most common
10         # configuration knobs. The rest can be controlled by editing
11         # tahoe.cfg before node startup.
12         ("nickname", "n", None, "nickname for this node"),
13         ("introducer", "i", None, "introducer FURL to use"),
14         ("webport", "p", "tcp:3456:interface=127.0.0.1",
15          "which TCP port to run the HTTP interface on. Use 'none' to disable."),
16         ]
17
18 class CreateNodeOptions(CreateClientOptions):
19     optFlags = [
20         ("no-storage", None, "do not offer storage service to other nodes"),
21         ]
22
23 class CreateIntroducerOptions(NoDefaultBasedirMixin, usage.Options):
24     optParameters = [
25         ["basedir", "C", None, "which directory to create the introducer in"],
26         ]
27
28 client_tac = """
29 # -*- python -*-
30
31 import pkg_resources
32 pkg_resources.require('allmydata-tahoe')
33 pkg_resources.require('twisted')
34 from allmydata import client
35 from twisted.application import service
36
37 c = client.Client()
38
39 application = service.Application("allmydata_client")
40 c.setServiceParent(application)
41 """
42
43 introducer_tac = """
44 # -*- python -*-
45
46 import pkg_resources
47 pkg_resources.require('allmydata-tahoe')
48 pkg_resources.require('twisted')
49 from allmydata import introducer
50 from twisted.application import service
51
52 c = introducer.IntroducerNode()
53
54 application = service.Application("allmydata_introducer")
55 c.setServiceParent(application)
56 """
57
58 def write_node_config(c, config):
59     # this is shared between clients and introducers
60     c.write("# -*- mode: conf; coding: utf-8 -*-\n")
61     c.write("\n")
62     c.write("# This file controls the configuration of the Tahoe node that\n")
63     c.write("# lives in this directory. It is only read at node startup.\n")
64     c.write("# For details about the keys that can be set here, please\n")
65     c.write("# read the 'docs/configuration.txt' file that came with your\n")
66     c.write("# Tahoe installation.\n")
67     c.write("\n\n")
68
69     c.write("[node]\n")
70     c.write("nickname = %s\n" % config.get("nickname", "")) #TODO: utf8 in argv?
71     webport = config.get("webport", "none")
72     if webport.lower() == "none":
73         webport = ""
74     c.write("web.port = %s\n" % webport)
75     c.write("web.static = public_html\n")
76     c.write("#tub.port =\n")
77     c.write("#tub.location = \n")
78     c.write("#log_gatherer.furl =\n")
79     c.write("#timeout.keepalive =\n")
80     c.write("#timeout.disconnect =\n")
81     c.write("#ssh.port = 8022\n")
82     c.write("#ssh.authorized_keys_file = ~/.ssh/authorized_keys\n")
83     c.write("\n")
84
85
86 def create_node(basedir, config, out=sys.stdout, err=sys.stderr):
87     if os.path.exists(basedir):
88         if os.listdir(basedir):
89             print >>err, "The base directory \"%s\", which is \"%s\" is not empty." % (basedir, os.path.abspath(basedir))
90             print >>err, "To avoid clobbering anything, I am going to quit now."
91             print >>err, "Please use a different directory, or empty this one."
92             return -1
93         # we're willing to use an empty directory
94     else:
95         os.mkdir(basedir)
96     f = open(os.path.join(basedir, "tahoe-client.tac"), "w")
97     f.write(client_tac)
98     f.close()
99
100     c = open(os.path.join(basedir, "tahoe.cfg"), "w")
101
102     write_node_config(c, config)
103
104     c.write("[client]\n")
105     c.write("introducer.furl = %s\n" % config.get("introducer", ""))
106     c.write("helper.furl =\n")
107     c.write("#key_generator.furl =\n")
108     c.write("#stats_gatherer.furl =\n")
109     c.write("#shares.needed = 3\n")
110     c.write("#shares.happy = 7\n")
111     c.write("#shares.total = 10\n")
112     c.write("\n")
113
114     boolstr = {True:"true", False:"false"}
115     c.write("[storage]\n")
116     storage_enabled = not config.get("no-storage", None)
117     c.write("enabled = %s\n" % boolstr[storage_enabled])
118     c.write("#readonly =\n")
119     c.write("#reserved_space =\n")
120     c.write("#expire.enabled =\n")
121     c.write("#expire.mode =\n")
122     c.write("\n")
123
124     c.write("[helper]\n")
125     c.write("enabled = false\n")
126     c.write("\n")
127
128     c.close()
129
130     from allmydata.util import fileutil
131     fileutil.make_dirs(os.path.join(basedir, "private"), 0700)
132     print >>out, "Node created in %s" % basedir
133     if not config.get("introducer", ""):
134         print >>out, " Please set [client]introducer.furl= in tahoe.cfg!"
135         print >>out, " The node cannot connect to a grid without it."
136     if not config.get("nickname", ""):
137         print >>out, " Please set [node]nickname= in tahoe.cfg"
138
139
140 def create_client(basedir, config, out=sys.stdout, err=sys.stderr):
141     config['no-storage'] = True
142     return create_node(basedir, config, out=out, err=err)
143
144
145 def create_introducer(basedir, config, out=sys.stdout, err=sys.stderr):
146     if os.path.exists(basedir):
147         if os.listdir(basedir):
148             print >>err, "The base directory \"%s\", which is \"%s\" is not empty." % (basedir, os.path.abspath(basedir))
149             print >>err, "To avoid clobbering anything, I am going to quit now."
150             print >>err, "Please use a different directory, or empty this one."
151             return -1
152         # we're willing to use an empty directory
153     else:
154         os.mkdir(basedir)
155     f = open(os.path.join(basedir, "tahoe-introducer.tac"), "w")
156     f.write(introducer_tac)
157     f.close()
158
159     c = open(os.path.join(basedir, "tahoe.cfg"), "w")
160     write_node_config(c, config)
161     c.close()
162
163     print >>out, "Introducer created in %s" % basedir
164
165
166 subCommands = [
167     ["create-node", None, CreateNodeOptions, "Create a node that acts as a client, server or both."],
168     ["create-client", None, CreateClientOptions, "Create a client node (with storage initially disabled)."],
169     ["create-introducer", None, CreateIntroducerOptions, "Create an introducer node."],
170 ]
171
172 dispatch = {
173     "create-node": create_node,
174     "create-client": create_client,
175     "create-introducer": create_introducer,
176     }