You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

servicecontext.go 4.3 kB

2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. package svc
  2. import (
  3. "github.com/aws/aws-sdk-go/aws"
  4. "github.com/aws/aws-sdk-go/aws/credentials"
  5. "github.com/aws/aws-sdk-go/aws/session"
  6. "github.com/aws/aws-sdk-go/service/s3/s3manager"
  7. "github.com/docker/docker/client"
  8. "github.com/redis/go-redis/v9"
  9. "github.com/robfig/cron/v3"
  10. "github.com/zeromicro/go-queue/kq"
  11. "github.com/zeromicro/go-zero/core/logx"
  12. "github.com/zeromicro/go-zero/zrpc"
  13. "gitlink.org.cn/jcce-pcm/pcm-coordinator/api/internal/config"
  14. "gitlink.org.cn/jcce-pcm/pcm-coordinator/pkg/utils"
  15. "gitlink.org.cn/jcce-pcm/pcm-participant-ac/hpcacclient"
  16. "gitlink.org.cn/jcce-pcm/pcm-participant-ceph/cephclient"
  17. "gitlink.org.cn/jcce-pcm/pcm-participant-kubernetes/kubernetesclient"
  18. "gitlink.org.cn/jcce-pcm/pcm-participant-modelarts/client/imagesservice"
  19. "gitlink.org.cn/jcce-pcm/pcm-participant-modelarts/client/modelartsservice"
  20. "gitlink.org.cn/jcce-pcm/pcm-participant-octopus/octopusclient"
  21. "gitlink.org.cn/jcce-pcm/pcm-participant-openstack/openstackclient"
  22. "gitlink.org.cn/jcce-pcm/pcm-participant-slurm/hpcthclient"
  23. "gorm.io/driver/mysql"
  24. "gorm.io/gorm"
  25. "gorm.io/gorm/schema"
  26. )
  27. type ServiceContext struct {
  28. Config config.Config
  29. RedisClient *redis.Client
  30. ScheduleHpcClient *kq.Pusher
  31. ScheduleCloudClient *kq.Pusher
  32. ScheduleAiClient *kq.Pusher
  33. Cron *cron.Cron
  34. ModelArtsRpc modelartsservice.ModelArtsService
  35. ModelArtsImgRpc imagesservice.ImagesService
  36. DbEngin *gorm.DB
  37. ACRpc hpcacclient.HpcAC
  38. THRpc hpcthclient.HpcTH
  39. OctopusRpc octopusclient.Octopus
  40. CephRpc cephclient.Ceph
  41. OpenstackRpc openstackclient.Openstack
  42. DockerClient *client.Client
  43. Downloader *s3manager.Downloader
  44. Uploader *s3manager.Uploader
  45. K8sRpc map[int64]kubernetesclient.Kubernetes
  46. }
  47. func NewServiceContext(c config.Config) *ServiceContext {
  48. // 创建s3 session
  49. session, _ := session.NewSession(&aws.Config{
  50. Credentials: credentials.NewStaticCredentials(c.MinioConf.AccessKey, c.MinioConf.Secret, ""), //使用静态凭据,硬编码
  51. Endpoint: aws.String(c.MinioConf.Endpoint), //配置端点
  52. Region: aws.String("default"), //配置区域
  53. DisableSSL: aws.Bool(false), //是否禁用https,这里表示不禁用,即使用HTTPS
  54. S3ForcePathStyle: aws.Bool(true), //使用路径样式而非虚拟主机样式,区别请参考:https://docs.aws.amazon.com/AmazonS3/latest/dev/VirtualHosting.html
  55. })
  56. //添加snowflake支持
  57. err := utils.InitSnowflake(c.SnowflakeConf.MachineId)
  58. if err != nil {
  59. logx.Errorf("InitSnowflake err: ", err)
  60. panic("InitSnowflake err")
  61. }
  62. downloader := s3manager.NewDownloader(session)
  63. uploader := s3manager.NewUploader(session)
  64. //启动Gorm支持
  65. dbEngin, _ := gorm.Open(mysql.Open(c.DB.DataSource), &gorm.Config{
  66. NamingStrategy: schema.NamingStrategy{
  67. SingularTable: true, // 使用单数表名,启用该选项,此时,`User` 的表名应该是 `t_user`
  68. },
  69. })
  70. dockerClient, err := client.NewClientWithOpts()
  71. if err != nil {
  72. logx.Error(err.Error())
  73. return nil
  74. }
  75. redisClient := redis.NewClient(&redis.Options{
  76. Addr: c.Redis.Host,
  77. Password: c.Redis.Pass,
  78. })
  79. return &ServiceContext{
  80. Cron: cron.New(cron.WithSeconds()),
  81. DbEngin: dbEngin,
  82. Config: c,
  83. RedisClient: redisClient,
  84. ModelArtsRpc: modelartsservice.NewModelArtsService(zrpc.MustNewClient(c.ModelArtsRpcConf)),
  85. ModelArtsImgRpc: imagesservice.NewImagesService(zrpc.MustNewClient(c.ModelArtsImgRpcConf)),
  86. CephRpc: cephclient.NewCeph(zrpc.MustNewClient(c.CephRpcConf)),
  87. ACRpc: hpcacclient.NewHpcAC(zrpc.MustNewClient(c.ACRpcConf)),
  88. OctopusRpc: octopusclient.NewOctopus(zrpc.MustNewClient(c.OctopusRpcConf)),
  89. OpenstackRpc: openstackclient.NewOpenstack(zrpc.MustNewClient(c.OpenstackRpcConf)),
  90. K8sRpc: make(map[int64]kubernetesclient.Kubernetes),
  91. DockerClient: dockerClient,
  92. Downloader: downloader,
  93. Uploader: uploader,
  94. }
  95. }

PCM is positioned as Software stack over Cloud, aiming to build the standards and ecology of heterogeneous cloud collaboration for JCC in a non intrusive and autonomous peer-to-peer manner.