github.com/askholme/packer@v0.7.2-0.20140924152349-70d9566a6852/packer/rpc/environment.go (about) 1 package rpc 2 3 import ( 4 "github.com/mitchellh/packer/packer" 5 "log" 6 "net/rpc" 7 ) 8 9 // A Environment is an implementation of the packer.Environment interface 10 // where the actual environment is executed over an RPC connection. 11 type Environment struct { 12 client *rpc.Client 13 mux *muxBroker 14 } 15 16 // A EnvironmentServer wraps a packer.Environment and makes it exportable 17 // as part of a Golang RPC server. 18 type EnvironmentServer struct { 19 env packer.Environment 20 mux *muxBroker 21 } 22 23 type EnvironmentCliArgs struct { 24 Args []string 25 } 26 27 func (e *Environment) Builder(name string) (b packer.Builder, err error) { 28 var streamId uint32 29 err = e.client.Call("Environment.Builder", name, &streamId) 30 if err != nil { 31 return 32 } 33 34 client, err := newClientWithMux(e.mux, streamId) 35 if err != nil { 36 return nil, err 37 } 38 b = client.Builder() 39 return 40 } 41 42 func (e *Environment) Cache() packer.Cache { 43 var streamId uint32 44 if err := e.client.Call("Environment.Cache", new(interface{}), &streamId); err != nil { 45 panic(err) 46 } 47 48 client, err := newClientWithMux(e.mux, streamId) 49 if err != nil { 50 log.Printf("[ERR] Error getting cache client: %s", err) 51 return nil 52 } 53 return client.Cache() 54 } 55 56 func (e *Environment) Cli(args []string) (result int, err error) { 57 rpcArgs := &EnvironmentCliArgs{args} 58 err = e.client.Call("Environment.Cli", rpcArgs, &result) 59 return 60 } 61 62 func (e *Environment) Hook(name string) (h packer.Hook, err error) { 63 var streamId uint32 64 err = e.client.Call("Environment.Hook", name, &streamId) 65 if err != nil { 66 return 67 } 68 69 client, err := newClientWithMux(e.mux, streamId) 70 if err != nil { 71 return nil, err 72 } 73 return client.Hook(), nil 74 } 75 76 func (e *Environment) PostProcessor(name string) (p packer.PostProcessor, err error) { 77 var streamId uint32 78 err = e.client.Call("Environment.PostProcessor", name, &streamId) 79 if err != nil { 80 return 81 } 82 83 client, err := newClientWithMux(e.mux, streamId) 84 if err != nil { 85 return nil, err 86 } 87 p = client.PostProcessor() 88 return 89 } 90 91 func (e *Environment) Provisioner(name string) (p packer.Provisioner, err error) { 92 var streamId uint32 93 err = e.client.Call("Environment.Provisioner", name, &streamId) 94 if err != nil { 95 return 96 } 97 98 client, err := newClientWithMux(e.mux, streamId) 99 if err != nil { 100 return nil, err 101 } 102 p = client.Provisioner() 103 return 104 } 105 106 func (e *Environment) Ui() packer.Ui { 107 var streamId uint32 108 e.client.Call("Environment.Ui", new(interface{}), &streamId) 109 110 client, err := newClientWithMux(e.mux, streamId) 111 if err != nil { 112 log.Printf("[ERR] Error connecting to Ui: %s", err) 113 return nil 114 } 115 return client.Ui() 116 } 117 118 func (e *EnvironmentServer) Builder(name string, reply *uint32) error { 119 builder, err := e.env.Builder(name) 120 if err != nil { 121 return NewBasicError(err) 122 } 123 124 *reply = e.mux.NextId() 125 server := newServerWithMux(e.mux, *reply) 126 server.RegisterBuilder(builder) 127 go server.Serve() 128 return nil 129 } 130 131 func (e *EnvironmentServer) Cache(args *interface{}, reply *uint32) error { 132 cache := e.env.Cache() 133 134 *reply = e.mux.NextId() 135 server := newServerWithMux(e.mux, *reply) 136 server.RegisterCache(cache) 137 go server.Serve() 138 return nil 139 } 140 141 func (e *EnvironmentServer) Cli(args *EnvironmentCliArgs, reply *int) (err error) { 142 *reply, err = e.env.Cli(args.Args) 143 return 144 } 145 146 func (e *EnvironmentServer) Hook(name string, reply *uint32) error { 147 hook, err := e.env.Hook(name) 148 if err != nil { 149 return NewBasicError(err) 150 } 151 152 *reply = e.mux.NextId() 153 server := newServerWithMux(e.mux, *reply) 154 server.RegisterHook(hook) 155 go server.Serve() 156 return nil 157 } 158 159 func (e *EnvironmentServer) PostProcessor(name string, reply *uint32) error { 160 pp, err := e.env.PostProcessor(name) 161 if err != nil { 162 return NewBasicError(err) 163 } 164 165 *reply = e.mux.NextId() 166 server := newServerWithMux(e.mux, *reply) 167 server.RegisterPostProcessor(pp) 168 go server.Serve() 169 return nil 170 } 171 172 func (e *EnvironmentServer) Provisioner(name string, reply *uint32) error { 173 prov, err := e.env.Provisioner(name) 174 if err != nil { 175 return NewBasicError(err) 176 } 177 178 *reply = e.mux.NextId() 179 server := newServerWithMux(e.mux, *reply) 180 server.RegisterProvisioner(prov) 181 go server.Serve() 182 return nil 183 } 184 185 func (e *EnvironmentServer) Ui(args *interface{}, reply *uint32) error { 186 ui := e.env.Ui() 187 188 *reply = e.mux.NextId() 189 server := newServerWithMux(e.mux, *reply) 190 server.RegisterUi(ui) 191 go server.Serve() 192 return nil 193 }