| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139 |
- // Copyright 2019 Yunion
- //
- // Licensed under the Apache License, Version 2.0 (the "License");
- // you may not use this file except in compliance with the License.
- // You may obtain a copy of the License at
- //
- // http://www.apache.org/licenses/LICENSE-2.0
- //
- // Unless required by applicable law or agreed to in writing, software
- // distributed under the License is distributed on an "AS IS" BASIS,
- // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- // See the License for the specific language governing permissions and
- // limitations under the License.
- package deployclient
- import (
- "context"
- "net"
- "time"
- "google.golang.org/grpc"
- deployapi "yunion.io/x/onecloud/pkg/hostman/hostdeployer/apis"
- )
- var (
- deployClient *DeployClient
- _ deployapi.DeployAgentClient = deployClient
- )
- func Init(socketPath string) {
- deployClient = NewDeployClient(socketPath)
- }
- type DeployClient struct {
- socketPath string
- }
- func NewDeployClient(socketPath string) *DeployClient {
- return &DeployClient{socketPath}
- }
- func GetDeployClient() *DeployClient {
- return deployClient
- }
- func grcpDialWithUnixSocket(ctx context.Context, socketPath string) (*grpc.ClientConn, error) {
- return grpc.DialContext(ctx, socketPath, grpc.WithInsecure(), grpc.WithBlock(), grpc.WithTimeout(time.Second*3),
- grpc.WithDialer(func(addr string, timeout time.Duration) (net.Conn, error) {
- return net.DialTimeout("unix", addr, timeout)
- }),
- )
- }
- func (c *DeployClient) DeployGuestFs(ctx context.Context, in *deployapi.DeployParams, opts ...grpc.CallOption) (*deployapi.DeployGuestFsResponse, error) {
- conn, err := grcpDialWithUnixSocket(ctx, c.socketPath)
- if err != nil {
- return nil, err
- }
- defer conn.Close()
- client := deployapi.NewDeployAgentClient(conn)
- ret, err := client.DeployGuestFs(ctx, in, opts...)
- return ret, err
- }
- func (c *DeployClient) ResizeFs(ctx context.Context, in *deployapi.ResizeFsParams, opts ...grpc.CallOption) (*deployapi.Empty, error) {
- conn, err := grcpDialWithUnixSocket(ctx, c.socketPath)
- if err != nil {
- return nil, err
- }
- defer conn.Close()
- client := deployapi.NewDeployAgentClient(conn)
- return client.ResizeFs(ctx, in, opts...)
- }
- func (c *DeployClient) FormatFs(ctx context.Context, in *deployapi.FormatFsParams, opts ...grpc.CallOption) (*deployapi.Empty, error) {
- conn, err := grcpDialWithUnixSocket(ctx, c.socketPath)
- if err != nil {
- return nil, err
- }
- defer conn.Close()
- client := deployapi.NewDeployAgentClient(conn)
- return client.FormatFs(ctx, in, opts...)
- }
- func (c *DeployClient) SaveToGlance(ctx context.Context, in *deployapi.SaveToGlanceParams, opts ...grpc.CallOption) (*deployapi.SaveToGlanceResponse, error) {
- conn, err := grcpDialWithUnixSocket(ctx, c.socketPath)
- if err != nil {
- return nil, err
- }
- defer conn.Close()
- client := deployapi.NewDeployAgentClient(conn)
- return client.SaveToGlance(ctx, in, opts...)
- }
- func (c *DeployClient) ProbeImageInfo(ctx context.Context, in *deployapi.ProbeImageInfoPramas, opts ...grpc.CallOption) (*deployapi.ImageInfo, error) {
- conn, err := grcpDialWithUnixSocket(ctx, c.socketPath)
- if err != nil {
- return nil, err
- }
- defer conn.Close()
- client := deployapi.NewDeployAgentClient(conn)
- return client.ProbeImageInfo(ctx, in, opts...)
- }
- func (c *DeployClient) ConnectEsxiDisks(
- ctx context.Context, in *deployapi.ConnectEsxiDisksParams, opts ...grpc.CallOption,
- ) (*deployapi.EsxiDisksConnectionInfo, error) {
- conn, err := grcpDialWithUnixSocket(ctx, c.socketPath)
- if err != nil {
- return nil, err
- }
- defer conn.Close()
- client := deployapi.NewDeployAgentClient(conn)
- return client.ConnectEsxiDisks(ctx, in, opts...)
- }
- func (c *DeployClient) DisconnectEsxiDisks(
- ctx context.Context, in *deployapi.EsxiDisksConnectionInfo, opts ...grpc.CallOption,
- ) (*deployapi.Empty, error) {
- conn, err := grcpDialWithUnixSocket(ctx, c.socketPath)
- if err != nil {
- return nil, err
- }
- defer conn.Close()
- client := deployapi.NewDeployAgentClient(conn)
- return client.DisconnectEsxiDisks(ctx, in, opts...)
- }
- func (c *DeployClient) SetOvmfBootOrder(ctx context.Context, in *deployapi.OvmfBootOrderParams, opts ...grpc.CallOption) (*deployapi.Empty, error) {
- conn, err := grcpDialWithUnixSocket(ctx, c.socketPath)
- if err != nil {
- return nil, err
- }
- defer conn.Close()
- client := deployapi.NewDeployAgentClient(conn)
- return client.SetOvmfBootOrder(ctx, in, opts...)
- }
|