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