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