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