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