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.1 kB

2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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/go-redis/redis"
  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-modelarts/modelartsclient"
  18. "gitlink.org.cn/jcce-pcm/pcm-participant-octopus/octopusclient"
  19. "gitlink.org.cn/jcce-pcm/pcm-participant-openstack/openstackclient"
  20. "gitlink.org.cn/jcce-pcm/pcm-participant-slurm/hpcthclient"
  21. "gorm.io/driver/mysql"
  22. "gorm.io/gorm"
  23. "gorm.io/gorm/schema"
  24. )
  25. type ServiceContext struct {
  26. Config config.Config
  27. RedisClient *redis.Client
  28. ScheduleHpcClient *kq.Pusher
  29. ScheduleCloudClient *kq.Pusher
  30. ScheduleAiClient *kq.Pusher
  31. Cron *cron.Cron
  32. ModelArtsRpc modelartsclient.ModelArts
  33. DbEngin *gorm.DB
  34. ACRpc hpcacclient.HpcAC
  35. THRpc hpcthclient.HpcTH
  36. OctopusRpc octopusclient.Octopus
  37. CephRpc cephclient.Ceph
  38. OpenstackRpc openstackclient.Openstack
  39. DockerClient *client.Client
  40. Downloader *s3manager.Downloader
  41. Uploader *s3manager.Uploader
  42. }
  43. func NewServiceContext(c config.Config) *ServiceContext {
  44. // 创建s3 session
  45. session, _ := session.NewSession(&aws.Config{
  46. Credentials: credentials.NewStaticCredentials(c.MinioConf.AccessKey, c.MinioConf.Secret, ""), //使用静态凭据,硬编码
  47. Endpoint: aws.String(c.MinioConf.Endpoint), //配置端点
  48. Region: aws.String("default"), //配置区域
  49. DisableSSL: aws.Bool(false), //是否禁用https,这里表示不禁用,即使用HTTPS
  50. S3ForcePathStyle: aws.Bool(true), //使用路径样式而非虚拟主机样式,区别请参考:https://docs.aws.amazon.com/AmazonS3/latest/dev/VirtualHosting.html
  51. })
  52. //添加snowflake支持
  53. err := utils.InitSnowflake(c.SnowflakeConf.MachineId)
  54. if err != nil {
  55. logx.Errorf("InitSnowflake err: ", err)
  56. panic("InitSnowflake err")
  57. }
  58. downloader := s3manager.NewDownloader(session)
  59. uploader := s3manager.NewUploader(session)
  60. //启动Gorm支持
  61. dbEngin, _ := gorm.Open(mysql.Open(c.DB.DataSource), &gorm.Config{
  62. NamingStrategy: schema.NamingStrategy{
  63. SingularTable: true, // 使用单数表名,启用该选项,此时,`User` 的表名应该是 `t_user`
  64. },
  65. })
  66. dockerClient, err := client.NewClientWithOpts()
  67. if err != nil {
  68. logx.Error(err.Error())
  69. return nil
  70. }
  71. return &ServiceContext{
  72. Cron: cron.New(cron.WithSeconds()),
  73. DbEngin: dbEngin,
  74. Config: c,
  75. RedisClient: redis.NewClient(&redis.Options{
  76. Addr: c.Redis.Host,
  77. Password: c.Redis.Pass,
  78. }),
  79. ScheduleHpcClient: kq.NewPusher(c.KqProducerConf.Brokers, c.KqProducerConf.HpcTopic),
  80. ScheduleCloudClient: kq.NewPusher(c.KqProducerConf.Brokers, c.KqProducerConf.CloudTopic),
  81. ScheduleAiClient: kq.NewPusher(c.KqProducerConf.Brokers, c.KqProducerConf.AiTopic),
  82. ModelArtsRpc: modelartsclient.NewModelArts(zrpc.MustNewClient(c.ModelArtsRpcConf)),
  83. CephRpc: cephclient.NewCeph(zrpc.MustNewClient(c.CephRpcConf)),
  84. ACRpc: hpcacclient.NewHpcAC(zrpc.MustNewClient(c.ACRpcConf)),
  85. OctopusRpc: octopusclient.NewOctopus(zrpc.MustNewClient(c.OctopusRpcConf)),
  86. OpenstackRpc: openstackclient.NewOpenstack(zrpc.MustNewClient(c.OpenstackRpcConf)),
  87. DockerClient: dockerClient,
  88. Downloader: downloader,
  89. Uploader: uploader,
  90. }
  91. }

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.