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