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