deployclient.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. // Copyright 2019 Yunion
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package deployclient
  15. import (
  16. "context"
  17. "net"
  18. "time"
  19. "google.golang.org/grpc"
  20. deployapi "yunion.io/x/onecloud/pkg/hostman/hostdeployer/apis"
  21. )
  22. var (
  23. deployClient *DeployClient
  24. _ deployapi.DeployAgentClient = deployClient
  25. )
  26. func Init(socketPath string) {
  27. deployClient = NewDeployClient(socketPath)
  28. }
  29. type DeployClient struct {
  30. socketPath string
  31. }
  32. func NewDeployClient(socketPath string) *DeployClient {
  33. return &DeployClient{socketPath}
  34. }
  35. func GetDeployClient() *DeployClient {
  36. return deployClient
  37. }
  38. func grcpDialWithUnixSocket(ctx context.Context, socketPath string) (*grpc.ClientConn, error) {
  39. return grpc.DialContext(ctx, socketPath, grpc.WithInsecure(), grpc.WithBlock(), grpc.WithTimeout(time.Second*3),
  40. grpc.WithDialer(func(addr string, timeout time.Duration) (net.Conn, error) {
  41. return net.DialTimeout("unix", addr, timeout)
  42. }),
  43. )
  44. }
  45. func (c *DeployClient) DeployGuestFs(ctx context.Context, in *deployapi.DeployParams, opts ...grpc.CallOption) (*deployapi.DeployGuestFsResponse, error) {
  46. conn, err := grcpDialWithUnixSocket(ctx, c.socketPath)
  47. if err != nil {
  48. return nil, err
  49. }
  50. defer conn.Close()
  51. client := deployapi.NewDeployAgentClient(conn)
  52. ret, err := client.DeployGuestFs(ctx, in, opts...)
  53. return ret, err
  54. }
  55. func (c *DeployClient) ResizeFs(ctx context.Context, in *deployapi.ResizeFsParams, opts ...grpc.CallOption) (*deployapi.Empty, error) {
  56. conn, err := grcpDialWithUnixSocket(ctx, c.socketPath)
  57. if err != nil {
  58. return nil, err
  59. }
  60. defer conn.Close()
  61. client := deployapi.NewDeployAgentClient(conn)
  62. return client.ResizeFs(ctx, in, opts...)
  63. }
  64. func (c *DeployClient) FormatFs(ctx context.Context, in *deployapi.FormatFsParams, opts ...grpc.CallOption) (*deployapi.Empty, error) {
  65. conn, err := grcpDialWithUnixSocket(ctx, c.socketPath)
  66. if err != nil {
  67. return nil, err
  68. }
  69. defer conn.Close()
  70. client := deployapi.NewDeployAgentClient(conn)
  71. return client.FormatFs(ctx, in, opts...)
  72. }
  73. func (c *DeployClient) SaveToGlance(ctx context.Context, in *deployapi.SaveToGlanceParams, opts ...grpc.CallOption) (*deployapi.SaveToGlanceResponse, error) {
  74. conn, err := grcpDialWithUnixSocket(ctx, c.socketPath)
  75. if err != nil {
  76. return nil, err
  77. }
  78. defer conn.Close()
  79. client := deployapi.NewDeployAgentClient(conn)
  80. return client.SaveToGlance(ctx, in, opts...)
  81. }
  82. func (c *DeployClient) ProbeImageInfo(ctx context.Context, in *deployapi.ProbeImageInfoPramas, opts ...grpc.CallOption) (*deployapi.ImageInfo, error) {
  83. conn, err := grcpDialWithUnixSocket(ctx, c.socketPath)
  84. if err != nil {
  85. return nil, err
  86. }
  87. defer conn.Close()
  88. client := deployapi.NewDeployAgentClient(conn)
  89. return client.ProbeImageInfo(ctx, in, opts...)
  90. }
  91. func (c *DeployClient) ConnectEsxiDisks(
  92. ctx context.Context, in *deployapi.ConnectEsxiDisksParams, opts ...grpc.CallOption,
  93. ) (*deployapi.EsxiDisksConnectionInfo, error) {
  94. conn, err := grcpDialWithUnixSocket(ctx, c.socketPath)
  95. if err != nil {
  96. return nil, err
  97. }
  98. defer conn.Close()
  99. client := deployapi.NewDeployAgentClient(conn)
  100. return client.ConnectEsxiDisks(ctx, in, opts...)
  101. }
  102. func (c *DeployClient) DisconnectEsxiDisks(
  103. ctx context.Context, in *deployapi.EsxiDisksConnectionInfo, opts ...grpc.CallOption,
  104. ) (*deployapi.Empty, error) {
  105. conn, err := grcpDialWithUnixSocket(ctx, c.socketPath)
  106. if err != nil {
  107. return nil, err
  108. }
  109. defer conn.Close()
  110. client := deployapi.NewDeployAgentClient(conn)
  111. return client.DisconnectEsxiDisks(ctx, in, opts...)
  112. }
  113. func (c *DeployClient) SetOvmfBootOrder(ctx context.Context, in *deployapi.OvmfBootOrderParams, opts ...grpc.CallOption) (*deployapi.Empty, error) {
  114. conn, err := grcpDialWithUnixSocket(ctx, c.socketPath)
  115. if err != nil {
  116. return nil, err
  117. }
  118. defer conn.Close()
  119. client := deployapi.NewDeployAgentClient(conn)
  120. return client.SetOvmfBootOrder(ctx, in, opts...)
  121. }